Problem:


最大の和  

問題

n 個の整数からなる数列 a1, a2, ..., an と正整数 k (1 ≤ k ≤ n) が与えられる.このとき, 連続して並ぶ k 個の整数の和  Si = ai + ai+1 + ... + ai+k-1 (1 ≤ i ≤ n - k + 1) の最大値を出力するプログラムを作りなさい.

入力

input.txt である.-->
入力は複数のデータセットからなる．各データセットは以下の形式で与えられる．入力は２つのゼロを含む行で終了する．

 1 行目には正整数 n (1 ≤ n ≤  100000) と正整数 k (1 ≤ k ≤ n) がこの順に空白で
区切られて書かれている.2 行目以降の第 1 + i 行目 (1 ≤ i ≤ n) には, 数列の i 番目の項 ai (-10000 ≤ ai ≤ 10000) が書かれている.
 採点用データのうち, 配点の 60% 分は n ≤ 5000, k ≤ 1000 を満たす.

データセットの数は 5 を超えない．

出力

output.txt である.
output.txt は 1 行だけからなり,その 1 行は Si の最大値だけを含む.
-->
データセットごとにSi の最大値を１行に出力する．

入出力例

入力例

5 3
2
5
-4
10
3
0 0

出力例

11

上記問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。



Sample input:
5 3
2
5
-4
10
3
0 0


Sample output:
11

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

# 区間[N-k, N) の総和の最大値


n, k = map(int, input().split(" "))
a_n = [int(input()) for _ in range(n)]
_ = input()

a_n = list(accumulate(a_n))
# print(a_n)

result = -99999999999999

for i in range(n-k):
  val = a_n[k+i] - a_n[i]
  if result < val:
    result = val


print(result)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(input()) for i in range(n)]

unko = [0]
for i in range(n):
        unko.append(unko[i]+a[i])
mvl = 0
for i in range(n-k+1):
        if unko[i+k]-unko[i] >= mvl:
                mvl = unko[i+k]-unko[i]
print(mvl)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(input()) for i in range(n)]

unko = [0]
for i in range(n):
        unko.append(unko[i]+a[i])
Inf = float("inf")*(-1)
mvl = Inf
for i in range(n-k+1):
        if unko[i+k]-unko[i] >= mvl:
                mvl = unko[i+k]-unko[i]
print(mvl)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [int(input()) for i in range(n)]

unko = [0]
for i in range(n):
        unko.append(unko[i]+a[i])
inf = float("inf")*(-1)
mvl = inf
for i in range(n-k+1):
        if unko[i+k]-unko[i] >= mvl:
                mvl = unko[i+k]-unko[i]
print(mvl)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
import string
import sys
from itertools import chain, dropwhile, takewhile


def read(
    *shape, f=int, it=chain.from_iterable(sys.stdin), whitespaces=set(string.whitespace)
):
    def read_word():
        w = lambda c: c in whitespaces
        nw = lambda c: c not in whitespaces
        return f("".join(takewhile(nw, dropwhile(w, it))))

    if not shape:
        return read_word()
    elif len(shape) == 1:
        return [read_word() for _ in range(shape[0])]
    elif len(shape) == 2:
        return [[read_word() for _ in range(shape[1])] for _ in range(shape[0])]


def readi(*shape):
    return read(*shape)


def readi1(*shape):
    return [i - 1 for i in read(*shape)]


def readf(*shape):
    return read(*shape, f=float)


def reads(*shape):
    return read(*shape, f=str)


def arr(*shape, fill_value=0):
    if len(shape) == 1:
        return [fill_value] * shape[fill_value]
    elif len(shape) == 2:
        return [[fill_value] * shape[1] for _ in range(shape[0])]


def dbg(**kwargs):
    print(
        ", ".join("{} = {}".format(k, repr(v)) for k, v in kwargs.items()),
        file=sys.stderr,
    )


def main():
    n, k = readi(2)
    a = readi(n)
    tmp = sum(a[:k])
    ans = tmp
    for i in range(k, len(a)):
        ans = max(ans, tmp + a[i] - a[i - k])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
import string
import sys
from itertools import chain, dropwhile, takewhile


def read(
    *shape, f=int, it=chain.from_iterable(sys.stdin), whitespaces=set(string.whitespace)
):
    def read_word():
        w = lambda c: c in whitespaces
        nw = lambda c: c not in whitespaces
        return f("".join(takewhile(nw, dropwhile(w, it))))

    if not shape:
        return read_word()
    elif len(shape) == 1:
        return [read_word() for _ in range(shape[0])]
    elif len(shape) == 2:
        return [[read_word() for _ in range(shape[1])] for _ in range(shape[0])]


def readi(*shape):
    return read(*shape)


def readi1(*shape):
    return [i - 1 for i in read(*shape)]


def readf(*shape):
    return read(*shape, f=float)


def reads(*shape):
    return read(*shape, f=str)


def arr(*shape, fill_value=0):
    if len(shape) == 1:
        return [fill_value] * shape[fill_value]
    elif len(shape) == 2:
        return [[fill_value] * shape[1] for _ in range(shape[0])]


def dbg(**kwargs):
    print(
        ", ".join("{} = {}".format(k, repr(v)) for k, v in kwargs.items()),
        file=sys.stderr,
    )


def main():
    n, k = readi(2)
    a = readi(n)
    tmp = sum(a[:k])
    ans = tmp
    for i in range(k, len(a)):
        tmp += a[i] - a[i - k]
        ans = max(ans, tmp)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    [n, k] = [int(i) for i in input().split()]
    a = []
    for i in range(n):
        a.append(int(input()))

    old = sum(a[0:k])
    max = old

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

    print(max)

Submission status: Wrong Answer

Solution code:
def func(n,k):
    A = []
    ans = 0
    SUM = 0
    for i in range(n):
        A.append(int(input()))
    for i in range(k-1):
        SUM += A[i]
    for i in range(k,len(A)):
        ans = max(ans,SUM+A[i]-A[i-k])
    print(ans)

for i in range(10):
    n,k = [int(s) for s in input().split()]
    if n == 0 and k ==0:
        break
    else:
        func(n,k)

Submission status: Wrong Answer

Solution code:
def func(n,k):
    SUM = 0
    ans = 0
    A = [int(input()) for _ in range(n)]
    for i in range(k-1):
        SUM += A[i]
    for i in range(k,len(A)):
        ans = max(ans,SUM+A[i]-A[i-k])
        SUM = SUM+A[i]-A[i-k]
    print(ans)

for _ in range(10):
    n,k = [int(s) for s in input().split()]
    if n == 0 and k ==0:
        break
    else:
        func(n,k)

Submission status: Wrong Answer

Solution code:
n , o = map(int, input().split())
k = [0]
for i in range(n):
    k.append(k[-1]+int(input()))
m = -float("INF")
for i,l in zip(k,k[o:]):
    m = max(m,l-i)
print(m)

Submission status: Wrong Answer

Solution code:
from queue import deque

while True:
    n, k = map(int, input().split())
    if not n:
        break
    q = deque(int(input()) for _ in range(k))
    ans = sum(q)
    for _ in range(n - k):
        new = int(input())
        q.append(new)
        old = q.popleft()
        if new > old:
            ans += new - old
    print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=[int(input()) for _ in range(k)]
m=sum(a)
for i in range(n-k):
    a.append(int(input()))
    m=max(m,sum(a[-k:]))
print(m)

Submission status: Wrong Answer

Solution code:
while True:
    
    n,k = map(int,input().split())
    if n == k == 0:
        break
    N_lis = []
    for i in range(n):
        N_lis.append(int(input()))
    N_max = 0
    N_sum = 0
    for j in range(k):
        N_sum += N_lis[j]
    for l in range(n - k):
        if N_sum - N_lis[l] + N_lis[l + k] > N_sum:
            N_max = N_sum - N_lis[l] + N_lis[l + k]
        N_sum = N_sum - N_lis[l] + N_lis[l + k]
    print(N_max)

Submission status: Wrong Answer

Solution code:
def main():
	while True:
		a = []
		n, k = [int(x) for x in input().split()]
		if n == 0 and k == 0: break
		for i in range(n): a.append(int(input()))
		l = [0]
		for i in range(1, n - k + 1): l.append(a[i + k - 1] - a[i - 1])
		maxsum = 0
		maxi = 0
		s = 0
		for i, x in enumerate(l):
			s += x
			if s >= maxsum:
				maxsum = s
				maxi = i
		print(sum(a[i - 1:i + k - 1]))
main()

Submission status: Wrong Answer

Solution code:
def main():
	while True:
		a = []
		n, k = [int(x) for x in input().split()]
		if n == 0 and k == 0: break
		for i in range(n): a.append(int(input()))
		l = [0]
		for i in range(1, n - k + 2): l.append(a[i + k - 2] - a[i - 1])
		maxsum = 0
		maxi = 0
		s = 0
		for i, x in enumerate(l):
			s += x
			if s >= maxsum:
				maxsum = s
				maxi = i
		print(sum(a[i - 1:i + k - 1]))
main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0516

"""
import sys
from sys import stdin
from collections import deque
input = stdin.readline


def main(args):
    while True:
        n, k = map(int, input().split())
        if n == 0 and k == 0:
            break

        d = deque([int(input()) for _ in range(k)])
        sum_value = sum(d)
        maximum_sum = sum_value

        for _ in range(n-k):
            v = int(input())
            o = d.popleft()
            if sum_value - o + v > maximum_sum:
                maximum_sum = sum_value - o + v
            d.append(v)

        print(maximum_sum)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
n,k = [int(i) for i in input().split()]
a = [int(input()) for i in range(n)]
b = [0]*len(a)

for i in range(n):
    b[i] = b[i-1] + a[i]
b = b + [0]

c = [b[i + k - 1] - b[i - 1] for i in range(n - k)]

print(max(c))

Submission status: Wrong Answer

Solution code:
n,k = [int(i) for i in input().split()]
a = [int(input()) for i in range(n)]
b = [0]*len(a)

for i in range(n):
    b[i] = b[i-1] + a[i]
b = b + [0]

c = [b[i + k - 1] - b[i - 1] for i in range(n - k + 1)]

print(max(c))

Submission status: Wrong Answer

Solution code:
while True:
    n,k=map(int,raw_input().split())
    if n==k==0:break
    L=[input() for i in range(n)]
    ma=0
    for i in range(n-2):
        ma=max(sum(L[i:i+3]),ma)
    print ma

Submission status: Wrong Answer

Solution code:
while True:
    n,k=map(int,raw_input().split())
    if n==k==0:break
    L=[input() for i in range(n)]
    maxInt=sum(L[:3])
    for i in range(3,n):
        maxInt=max(maxInt,maxInt+L[i]-L[i-3])
    print maxInt

Submission status: Wrong Answer

Solution code:
import sys

#fp = open("input.txt", "r")
fp = sys.stdin

while True:
    req = map(int, fp.readline()[:-1].split(" "))
    if req[0] + req[1] == 0:
        break

    n = req[0]
    k = req[1]

    sum_max = 0
    num_list = []
    
    for i in xrange(n):
        num = int(fp.readline()[:-1])
        num_list.append(num)
        if k > 0:
            sum_max += num
            k -= 1
            continue
        else:
            num_base = num_list.pop(0)
            if num > num_base:
                sum_max += num - num_base

    print sum_max

Submission status: Wrong Answer

Solution code:
import sys

#fp = open("input.txt", "r")
fp = sys.stdin

while True:
    req = map(int, fp.readline()[:-1].split(" "))
    if req[0] + req[1] == 0:
        break

    n = req[0]
    k = req[1]

    sum_max = 0
    num_list = []
    
    for i in xrange(n):
        num = int(fp.readline()[:-1])
        num_list.append(num)
        if k > 0:
            sum_max += num
            k -= 1
            continue
        else:
            sum_tmp = sum_max + num - num_list.pop(0)
            if sum_tmp > sum_max:
                sum_max = sum_tmp

    print sum_max

Submission status: Time Limit Exceeded

Solution code:
while True:
    a, b = map(int, input().split(" "))
    if a == 0 & b == 0:
        break
    li1 = []
    for i in range(0, a):
        li1.append(int(input()))
    max = 0
    for n in range(0, a + 1 - b):
        sum = 0
        for n1 in range(n, n + b):
            sum = sum + li1[n1]
        if max < sum:
            max = sum
    print(sum)

Submission status: Time Limit Exceeded

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

    if n == 0 and k == 0:
        break

    a = []
    for i in range(n):
        a.append(int(input()))

    list1 = []
    try:
        for i in range(n):
            num = 0
            for j in range(k):
                num += a[i + j]
            list1.append(num)
    except:
        pass

    print(max(list1))

Submission status: Time Limit Exceeded

Solution code:
while 1:                                       
    n, k = (int(x) for x in input().split())   
    if n == 0:                                 
        break                                  
    max_num = -30001                           
    queue = list()                             
    for _ in range(n):                         
        queue.append(int(input()))             
        if len(queue) == k:                    
            max_num = max(sum(queue), max_num) 
            queue.pop(0)                       
    print(max_num)

Submission status: Time Limit Exceeded

Solution code:
def main():
    while 1:
        maximum = 0
        n,k = map(int,input().split())
        if not n and not k: break
        N = [int(input())for _ in range(n)]
        for i in range(n-k+1):
            maximum = max(maximum,sum(N[i:i+k]))
        print(maximum)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
while 1:
    n,k=map(int,input().split())
    if n==0:break
    a=[int(input()) for _ in range(k)]
    m=sum(a)
    for i in range(n-k):
        a.append(int(input()))
        m=max(m,sum(a[-k:]))
    print(m)

Submission status: Time Limit Exceeded

Solution code:
import queue
while 1:
    n,k=map(int,input().split())
    if n==0:break
    a=queue.deque(int(input()) for _ in range(k))
    m=sum(a)
    for i in range(n-k):
        a.popleft()
        a.append(int(input()))
        m=max(m,sum(a))
    print(m)

Submission status: Time Limit Exceeded

Solution code:
while True:
    n, k = map(int, input().split())
    if n==0:break
    a = [int(input()) for _ in range(n)]
    print(max([sum(a[i:i+k]) for i in range(n) if len(a[i:i+k])==k]))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
while True:
    n, k = map(int, input().split())
    if n==0:break
    a = deque(int(input()) for _ in range(k))
    ans = sum(a)
    for _ in range(n-k):
        a.popleft()
        a.append(int(input()))
        tmp = sum(a)
        if ans < tmp: ans = tmp
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
while True:
    n,k=map(int,raw_input().split())
    if n==k==0:break
    L=[input() for i in range(n)]
    ma=0
    for i in range(n-k+1):
        ma=max(sum(L[i:i+k]),ma)
    print ma

Submission status: Time Limit Exceeded

Solution code:
while True:
    n,k=map(int,raw_input().split())
    if n==k==0:break
    L=[input() for i in range(n)]
    print sorted(sum(L[i:i+k]) for i in range(n-k+1))[-1]

Submission status: Time Limit Exceeded

Solution code:
while True:
    n,k=map(int,raw_input().split())
    if n==k==0:break
    L=[input() for i in range(n)]
    maxInt=-100000
    for i in range(n-k):
        maxInt=max(maxInt,sum(L[i:i+k]))
    print maxInt

Submission status: Time Limit Exceeded

Solution code:
import sys

#fp = open("input.txt", "r")
fp = sys.stdin

while True:
    req = map(int, fp.readline()[:-1].split(" "))
    if req[0] + req[1] == 0:
        break

    n = req[0]
    k = req[1]

    sum_max = 0
    num_list = []
    
    for i in xrange(n):
        num = int(fp.readline()[:-1])
        num_list.append(num)
        if k > 0:
            sum_max += num
            k -= 1
            continue
        else:
            num_list.pop(0)
            sum_tmp = sum(num_list)
            if sum_tmp > sum_max:
                sum_max = sum_tmp

    print sum_max

Submission status: Time Limit Exceeded

Solution code:
while 1:
	n,k=map(int,raw_input().split())
	if n==0:break
	a=[int(raw_input()) for i in range(n)]
	ans=-999999999
	for i in range(n-k):
		ans=max(ans,sum(a[i:i+k]))
	print ans

Submission status: Time Limit Exceeded

Solution code:
while 1:
	n,k=map(int,raw_input().split())
	if n==0:break
	a=[int(raw_input()) for i in range(n)]
	b=[0]*(n-k)
	for ki in range(k):
		for i in range(n-k):
			b[i]+=a[i+ki]
	print max(b)

Submission status: Accepted

Solution code:
for v in range(5):
    n, k = map(int, input().split())
    if n == 0 and k == 0:
        break
    a = [int(input()) for i in range(n)]
    sum = []
    s = [0] * n

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

    for i in range(n-k):
        sum.append(s[i+k] - s[i])

    print(max(sum))

Submission status: Accepted

Solution code:
for v in range(5):
    n, k = map(int, input().split())
    if n == 0 and k == 0:
        break
    a = [int(input()) for i in range(n)]
    sum = []
    s = [0] * (n+1)

    for i in range(n):
        s[i+1] = s[i] + a[i]
    
    for i in range(n-k):
        sum.append(s[i+k] - s[i])

    print(max(sum))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import string
import sys
from itertools import chain, dropwhile, takewhile


def read(
    *shape, f=int, it=chain.from_iterable(sys.stdin), whitespaces=set(string.whitespace)
):
    def read_word():
        w = lambda c: c in whitespaces
        nw = lambda c: c not in whitespaces
        return f("".join(takewhile(nw, dropwhile(w, it))))

    if not shape:
        return read_word()
    elif len(shape) == 1:
        return [read_word() for _ in range(shape[0])]
    elif len(shape) == 2:
        return [[read_word() for _ in range(shape[1])] for _ in range(shape[0])]


def readi(*shape):
    return read(*shape)


def readi1(*shape):
    return [i - 1 for i in read(*shape)]


def readf(*shape):
    return read(*shape, f=float)


def reads(*shape):
    return read(*shape, f=str)


def arr(*shape, fill_value=0):
    if len(shape) == 1:
        return [fill_value] * shape[fill_value]
    elif len(shape) == 2:
        return [[fill_value] * shape[1] for _ in range(shape[0])]


def dbg(**kwargs):
    print(
        ", ".join("{} = {}".format(k, repr(v)) for k, v in kwargs.items()),
        file=sys.stderr,
    )


def main():
    while True:
        n, k = readi(2)
        if n == 0:
            return
        a = readi(n)
        tmp = sum(a[:k])
        ans = tmp
        for i in range(k, len(a)):
            tmp += a[i] - a[i - k]
            ans = max(ans, tmp)
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    while True:
        [n, k] = [int(i) for i in input().split()]
        if n == 0 and k == 0:
            break
        a = []
        for i in range(n):
            a.append(int(input()))

        old = sum(a[0:k])
        max = old

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

        print(max)

Submission status: Accepted

Solution code:
def fun(n, k):
    A = [int(input()) for _ in range(n)]
    k_sum = 0
    maximam = 0
    for num, a in enumerate(A):
        if 0 <= num <= k-1:
            k_sum += a
        else:
            maximam = max(maximam, k_sum + a - A[num-k])
            k_sum = k_sum + a - A[num-k]
    print(maximam)

for _ in range(5):
    n, k = map(int, input().split())
    if n == 0 and k == 0:
        break
    else:
        fun(n, k)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    while True:
        [n, k] = [int(i) for i in input().split()]
        if n == 0 and k == 0:
            break
        a = []
        for i in range(n):
            a.append(int(input()))

        old = sum(a[0:k])
        ans = old

        for i in range(1, n-k+1):
            new = old - a[i-1] + a[i+k-1]
            ans = max(ans, new)
            old = new

        print(ans)

Submission status: Accepted

Solution code:
def fun(n, k):
    A = []
    total = 0
    for _ in range(n):
        a = int(input())
        total += a
        A.append(total)
    maximam = A[k-1]
    for num, a in zip(range(k,n), A[k:]):
        maximam = max(maximam, A[num] - A[num - k])
    print(maximam)

for _ in range(5):
    n, k = map(int, input().split())
    if n == 0 and k == 0:
        break
    else:
        fun(n, k)

Submission status: Accepted

Solution code:
def func(n,k):
    SUM = 0
    ans = 0
    A = [int(input()) for _ in range(n)]
    for i in range(k):
        SUM += A[i]
    for i in range(k,len(A)):
        ans = max(ans,SUM+A[i]-A[i-k])
        SUM = SUM+A[i]-A[i-k]
    print(ans)

for _ in range(10):
    n,k = [int(s) for s in input().split()]
    if n == 0 and k ==0:
        break
    else:
        func(n,k)

Submission status: Accepted

Solution code:
while 1:
    n , o = map(int, input().split())
    if n == 0 and o == 0:break
    else:
        k = [0]
        for i in range(n):
            k.append(k[-1]+int(input()))
        m = -float("INF")
        for i,l in zip(k,k[o:]):
            m = max(m,l-i)
        print(m)

Submission status: Accepted

Solution code:
while(1):
    [n,k]=[int(x) for x in raw_input().split()]
    if n==0:
        break
    else:
        a=[]
        for i in range(n):
            a.append(int(raw_input()))
        sumnow=sum(a[0:k])
        M=sumnow
        for top in range(1,n-k+1):
            sumnow=sumnow-a[top-1]+a[top-1+k]
            M=max(sumnow,M)
        print M

Submission status: Accepted

Solution code:
while True:    
    n, k = map(int, raw_input().split())
    if n==0: break
    t = [input()]
    sum = t[0]
    ans = -(1<<31)
    for i in range(1,k):
        t += [input()]
        sum += t[i]
    for i in range(k,n):
        t += [input()]
        sum += t[i] - t[i-k]
        ans = max(ans, sum)
    print ans

Submission status: Accepted

Solution code:
from queue import deque

while True:
    n, k = map(int, input().split())
    if not n:
        break
    q = deque(int(input()) for _ in range(k))
    ans = cur = sum(q)
    for _ in range(n - k):
        new = int(input())
        q.append(new)
        old = q.popleft()
        cur += new - old
        ans = ans if ans > cur else cur
    print(ans)

Submission status: Accepted

Solution code:
import queue
while 1:
    n,k=map(int,input().split())
    if n==0:break
    a=queue.deque(int(input()) for _ in range(k))
    m=b=sum(a)
    for i in range(n-k):
        c=a.popleft()
        d=int(input())
        a.append(d)
        b+=d-c
        m=max(b,m)        
    print(m)

Submission status: Accepted

Solution code:
from collections import deque
while True:
    n, k = map(int, input().split())
    if n==0:break
    a = deque(int(input()) for _ in range(k))
    ans=tmp=sum(a)
    for _ in range(n-k):
        x = a.popleft()
        y = int(input())
        a.append(y)
        tmp += y - x
        ans = max(ans, tmp)
    print(ans)

Submission status: Accepted

Solution code:
while True:
    n,k = map(int,input().split())
    if n == k == 0:
        break
    N_lis = []
    for i in range(n):
        N_lis.append(int(input()))
    N_max = 0
    N_sum_1 = 0
    N_sum_2 = 0
    for j in range(k):
        N_sum_1 += N_lis[j]
    for l in range(n - k):
        N_sum_2 = N_sum_1 - N_lis[l] + N_lis[l + k]
        if N_sum_2 > N_max:
            N_max = N_sum_2
        N_sum_1 = N_sum_2
    print(N_max)

Submission status: Accepted

Solution code:
def main():
	while True:
		a = []
		n, k = [int(x) for x in input().split()]
		if n == 0 and k == 0: break
		for i in range(n): a.append(int(input()))
		l = [0]
		for i in range(1, n - k + 1): l.append(a[i + k - 1] - a[i - 1])
		maxsum = 0
		maxi = 0
		s = 0
		for i, x in enumerate(l):
			s += x
			if s >= maxsum:
				maxsum = s
				maxi = i
		print(sum(a[maxi:maxi + k]))
main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0516

"""
import sys
from sys import stdin
from collections import deque
input = stdin.readline


def main(args):
    while True:
        n, k = map(int, input().split())
        if n == 0 and k == 0:
            break

        d = deque([int(input()) for _ in range(k)])
        sum_value = sum(d)
        maximum_sum = sum_value

        for _ in range(n-k):
            v = int(input())
            o = d.popleft()
            sum_value = sum_value - o + v
            if sum_value > maximum_sum:
                maximum_sum = sum_value
            d.append(v)

        print(maximum_sum)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
while True:
    n,k=[int(s)for s in input().split(" ")]
    if n==k==0:break
    a=[int(input())for a in range(n)]
    s=0
    for i in range(k):s+=a[i]
    maxval=s
    for i in range(n-k):
        s+=a[i+k]-a[i]
        if s>maxval:maxval=s
    print(maxval)

Submission status: Accepted

Solution code:
while True:
  n,k = map(int,input().split())
  if n == 0 and k == 0:
    break
  lst = []
  for i in range(n):
    lst.append(int(input()))
  count = 0
  for i in range(k):
    count += lst[i]
  ans = count
  for i in range(k, n):
    count += lst[i]
    count -= lst[i - k]
    ans = max(ans,count)
  print(ans)

Submission status: Accepted

Solution code:
def main():
  while True:
    n,k = map(int,input().split())
    if n == 0 and k == 0:
      break
    lst = []
    for i in range(n):
      lst.append(int(input()))
    count = 0
    for i in range(k):
      count += lst[i]
    ans = count
    for i in range(k, n):
      count += lst[i]
      count -= lst[i - k]
      ans = max(ans,count)
    print(ans)

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
while 1:
    n,k = [int(i) for i in input().split()]
    if not (n and k):
        break
    a = [int(input()) for i in range(n)]
    b = [0]*len(a)

    for i in range(n):
        b[i] = b[i-1] + a[i]
    b = b + [0]

    c = [b[i + k - 1] - b[i - 1] for i in range(n - k + 1)]

    print(max(c))

Submission status: Accepted

Solution code:
# AOJ 0516: Maximum Sum
# Python3 2018.6.30 bal4u

a, s = [0]*100003, [0]*100003
while True:
	n, k = map(int, input().split())
	if n == 0: break
	s[0] = a[0] = int(input())
	for i in range(1, n):
		a[i] = int(input())
		s[i] = s[i-1] + a[i]
		if i >= k: s[i] -= a[i-k]
	ans = s[k-1]
	for i in range(k, n):
		if s[i] > ans: ans = s[i]
	print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/python

import sys
def readints():
    return map(int, sys.stdin.readline().split())


n, k = readints()
while n and k:
    data = [readints()[0] for i in range(n)]

    window = sum(data[:k])
    L = window
    for i in range(k, n):
        window = window + data[i] - data[i-k]
        if window > L:
            L = window
    print L
    n, k = readints()

Submission status: Accepted

Solution code:
while True:
    n,k=map(int,raw_input().split())
    if n==k==0:break
    L=[input() for i in range(n)]
    maxInt=-10000000
    temp=sum(L[:k])
    for i in range(k,n):
        maxInt=max(maxInt,temp)
        temp=temp+L[i]-L[i-k]
    print maxInt

Submission status: Accepted

Solution code:
import sys

#fp = open("input.txt", "r")
fp = sys.stdin

while True:
    req = map(int, fp.readline()[:-1].split(" "))
    if req[0] + req[1] == 0:
        break

    n = req[0]
    k = req[1]

    sum_max = 0
    sum_prev = 0
    num_list = []
    
    for i in xrange(n):
        num = int(fp.readline()[:-1])
        num_list.append(num)
        if k > 0:
            sum_max += num
            k -= 1
            sum_prev = sum_max
            continue
        else:
            sum_tmp = sum_prev + num - num_list.pop(0)
            sum_prev = sum_tmp
            if sum_tmp > sum_max:
                sum_max = sum_tmp

    print sum_max

Submission status: Accepted

Solution code:
while True:
    n, k = map(int, raw_input().split())
    if (n,k) == (0,0):
        break
    a = [int(raw_input()) for _ in range(n)]
    sum, r = 0, 0
    for i in range(n):
        if i < k:
            sum += a[i]
        else:
            sum -= a[i-k]
            sum += a[i]
            r = max((r, sum))
    print r

Submission status: Accepted

Solution code:
a = [0]*10**5
while 1:
    n,k = map(int, raw_input().split())
    if n==0 and k==0: break
    for i in range(n): a[i] = int(raw_input())
    S = sum(a[0:k]); ans = 0;
    for i in range(n-k):
        ans = max(ans, S)
        S += a[k+i]-a[i]
    print(str(ans))

Submission status: Accepted

Solution code:
while 1:
	n,k=map(int,raw_input().split())
	if n==0:break
	a=[int(raw_input()) for i in range(n)]
	cur=mx=sum(a[:k])
	for i in range(n-k):
		cur=cur-a[i]+a[i+k]
		mx=max(mx,cur)
	print mx

Submission status: Accepted

Solution code:
while(1):
    n,k=map(int,input().split())
    if n==0 and k==0:
        break
    else:
        s=[0]*(n+1)
        top=0
        for i in range(1,n+1):
            b=int(input())
            
            s[i]=s[i-1]+b

        for i in range(n-k+1):
            v=s[k+i]-s[i]
            if v>top:
                top=v
        print(top)

Submission status: Accepted

Solution code:
while True:
    n,k = map(int,input().split())
    if n == 0 and k == 0:break
    a = [int(input())for _ in range(n)]
    s = sum(a[0:k])
    ss = [s]
    for i in range(k,n):
        s = s+a[i]-a[i-k]
        ss.append(s)
    print(max(ss))

Submission status: Accepted

Solution code:
while True:
    n,m=map(int,input().split())
    if n==0 and m==0:
        break
    A=[]
    for i in range(n):
        B=int(input())
        A.append(B)
    C=sum(A[0:m])
    D=C
    for i in range(m,n):
        C=C+A[i]-A[i-m]
        if C>D:
            D=C
    print(D)

Submission status: Accepted

Solution code:
while 1:
	n,m=map(int,input().split())
	if n==n==0: break
	li=[]
	for i in range(n):
		li.append(int(input()))
	s_li=[li[0]]
	for i in range(1,n):
		s_li.append(s_li[-1]+li[i])
	c_li=[s_li[m-1]]
	for i in range(n-m):
		c_li.append(s_li[i+m]-s_li[i])
	print(max(c_li))

Submission status: Accepted

Solution code:
while True:
    n,k=map(int,input().split())
    if k==0 and n==0:break
    a=[int(input()) for i in range(n)]
    s=sum(a[0:k])
    max=s
    for i in range(k,n):
        s=s+a[i]-a[i-k]
        if s>max:max=s
    print(max)

Submission status: Accepted

Solution code:
def main():
    while True:
        a,b=map(int,input().split())
        
        if a==0 and b==0:
            break
        l=[]
        for i in range(a):
            l.append(int(input()))
        count=0
        
        for i in range(b):
            count+=l[i]
        anser=count
        
        for i in range(b,a):
            count+=l[i]
            count-=l[i-b]
            anser=max(anser,count)
        print(anser)
        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
while True:
    n,k=map(int,input().split())
    if n==0 and k==0: 
        break
    a=[int(input()) for _ in range(n)]
    s=sum(a[0:k])
    ss=[s]
    for i in range(k,n):
        s=s+a[i]-a[i-k]
        ss.append(s)
    print(max(ss))

Submission status: Accepted

Solution code:
a=[]

while True:
    n,k=map(int,input().split())
    
    if n==0 and k==0:
        break
    
    maxa=0
    suma=0
    
    for i in range(n):
        a.append(int(input()))
        if i<k:
            suma +=a[i]
        else:
            suma +=a[i]-a[i-k]
        if i>k-1 and maxa<suma:
            maxa=suma
            
    print(maxa)

Submission status: Accepted

Solution code:
while True :
    n, k = map(int, input().split())
    
    if n == 0 :
        break
    else :
        A = []
        S = []
        s = 0
        cnt = 0
        
        for i in range(n) :
            A.append(int(input()))
            
        for i in range(k) :
            s += A[i]
        S.append(s)
            
        for j in range(k, n) :
            s = S[cnt] - A[j - k] + A[j]
            S.append(s)
            cnt += 1
        
        print(max(S))