CodeEff/ECCO huggingface dataset:
# Original solution
# chokudaiブログの典型力の話を意識しつつ考えたら思いついた!

# 「ある集合の部分集合の中で、〜〜〜の値が最小になるものを求めよ」のパターン
# 愚直にやると2^Nを調べねばならず無理。
# したがって「一部を調べればよい」に帰着するのが多いだろう。
# 何らかの形でソートしておいて、空集合から1つずつ付け加えるとか。全体集合から1つずつ取り去るとか。

# 今回は全体集合から1つずつ取り去るのを考えるとできる。
# 全体集合である競技m0が最多の人数p0人を集めたとする。
# すると、全ての部分集合のうち「m0を含む集合」を考えると、競技m0は必ずp0人以上集まるので解もp0以上。
# ここで「m0を含む集合」について一気に計算できていることに注意! 一撃で多数の集合について計算が終わる。
# 次に全体集合からm0を取り去ったものを考える。
# ここである競技m1が最多の人数p1人を集めたとする。すると……以下同様。
# なお複数種目の競技が同率1位になった場合は全て一度に取り除いて良い。
# これで空集合までやって、最大が最小のものを出力すればOK! 

n, m = list(map(int, input().split()))
preference = [list([int(x) - 1 for x in input().split()]) for _ in range(n)] # 1-index → 0-index

ans = n
held_sports = set(range(m))

while held_sports:
    # 各競技に参加する人数を求める
    parti = [0] * m
    for person in range(n):
        for s in range(m):
            if preference[person][s] in held_sports:
                parti[preference[person][s]] += 1
                break
    
    temp = max(parti)
    ans = min(ans, temp)
    # 集合から最大人数を取った要素を除外
    for idx, par in enumerate(parti):
        if par == temp:
            held_sports.remove(idx)

print(ans)

# Optimized solution
# chokudaiブログの典型力の話を意識しつつ考えたら思いついた!

# 「ある集合の部分集合の中で、〜〜〜の値が最小になるものを求めよ」のパターン
# 愚直にやると2^Nを調べねばならず無理。
# したがって「一部を調べればよい」に帰着するのが多いだろう。
# 何らかの形でソートしておいて、空集合から1つずつ付け加えるとか。全体集合から1つずつ取り去るとか。

# 今回は全体集合から1つずつ取り去るのを考えるとできる。
# 全体集合である競技m0が最多の人数p0人を集めたとする。
# すると、全ての部分集合のうち「m0を含む集合」を考えると、競技m0は必ずp0人以上集まるので解もp0以上。
# ここで「m0を含む集合」について一気に計算できていることに注意! 一撃で多数の集合について計算が終わる。
# 次に全体集合からm0を取り去ったものを考える。
# ここである競技m1が最多の人数p1人を集めたとする。すると……以下同様。
# なお複数種目の競技が同率1位になった場合は全て一度に取り除いて良い。
# これで空集合までやって、最大が最小のものを出力すればOK! 

n, m = list(map(int, input().split()))
preference = [list([int(x) - 1 for x in input().split()]) for _ in range(n)] # 1-index → 0-index

ans = n
held_sports = set(range(m))

while held_sports:
    # 各競技に参加する人数を求める
    parti = [0] * m
    for pref in preference:
        for s in range(m):
            if pref[s] in held_sports:
                parti[pref[s]] += 1
                break
    
    temp = max(parti)
    ans = min(ans, temp)
    # 集合から最大人数を取った要素を除外
    for idx, par in enumerate(parti):
        if par == temp:
            held_sports.remove(idx)

print(ans)
# Original solution
# chokudaiブログの典型力の話を意識しつつ考えたら思いついた!

# 「ある集合の部分集合の中で、〜〜〜の値が最小になるものを求めよ」のパターン
# 愚直にやると2^Nを調べねばならず無理。
# したがって「一部を調べればよい」に帰着するのが多いだろう。
# 何らかの形でソートしておいて、空集合から1つずつ付け加えるとか。全体集合から1つずつ取り去るとか。

# 今回は全体集合から1つずつ取り去るのを考えるとできる。
# 全体集合である競技m0が最多の人数p0人を集めたとする。
# すると、全ての部分集合のうち「m0を含む集合」を考えると、競技m0は必ずp0人以上集まるので解もp0以上。
# ここで「m0を含む集合」について一気に計算できていることに注意! 一撃で多数の集合について計算が終わる。
# 次に全体集合からm0を取り去ったものを考える。
# ここである競技m1が最多の人数p1人を集めたとする。すると……以下同様。
# なお複数種目の競技が同率1位になった場合は全て一度に取り除いて良い。
# これで空集合までやって、最大が最小のものを出力すればOK! 

n, m = list(map(int, input().split()))
preference = [list([int(x) - 1 for x in input().split()]) for _ in range(n)] # 1-index → 0-index

ans = n
held_sports = set(range(m))

while held_sports:
    # 各競技に参加する人数を求める
    parti = [0] * m
    for pref in preference:
        for s in pref:
            if s in held_sports:
                parti[s] += 1
                break
    
    temp = max(parti)
    ans = min(ans, temp)
    # 集合から最大人数を取った要素を除外
    for idx, par in enumerate(parti):
        if par == temp:
            held_sports.remove(idx)

print(ans)

# Optimized solution
# chokudaiブログの典型力の話を意識しつつ考えたら思いついた!

# 「ある集合の部分集合の中で、〜〜〜の値が最小になるものを求めよ」のパターン
# 愚直にやると2^Nを調べねばならず無理。
# したがって「一部を調べればよい」に帰着するのが多いだろう。
# 何らかの形でソートしておいて、空集合から1つずつ付け加えるとか。全体集合から1つずつ取り去るとか。

# 今回は全体集合から1つずつ取り去るのを考えるとできる。
# 全体集合である競技m0が最多の人数p0人を集めたとする。
# すると、全ての部分集合のうち「m0を含む集合」を考えると、競技m0は必ずp0人以上集まるので解もp0以上。
# ここで「m0を含む集合」について一気に計算できていることに注意! 一撃で多数の集合について計算が終わる。
# 次に全体集合からm0を取り去ったものを考える。
# ここである競技m1が最多の人数p1人を集めたとする。すると……以下同様。
# なお複数種目の競技が同率1位になった場合は全て一度に取り除いて良い。
# これで空集合までやって、最大が最小のものを出力すればOK! 

n, m = list(map(int, input().split()))
preference = [list([int(x) - 1 for x in input().split()]) for _ in range(n)] # 1-index → 0-index

ans = n
held_sports = set(range(m))

while held_sports:
    # 各競技に参加する人数を求める
    parti = [0] * m
    for pref in preference:
        for s in pref:
            if s in held_sports:
                parti[s] += 1
                break
    
    temp = max(parti)
    ans = min(ans, temp)
    # 集合から最大人数を取った要素を除外
    indices = [i for i,val in enumerate(parti) if val == temp]
    held_sports = held_sports - set(indices)

print(ans)
# Original solution
from collections import Counter

N, M = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(N)]

check = [0] * (M + 1)

answer = []
for _ in range(M):
  tmp = []
  for ai in A:
    for aij in ai:
      if check[aij] == 0:
        tmp.append(aij)
        break
  #print(tmp)
  counter = list(Counter(tmp).items())
  counter.sort(key = lambda x: -x[1])  
  answer.append(counter[0][1])
  check[counter[0][0]] = 1
  
print((min(answer)))

# Optimized solution
from collections import Counter

N, M = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(N)]

check = [0] * (M + 1)

answer = []
for _ in range(M):
  tmp = []
  for ai in A:
    for aij in ai:
      if check[aij] == 0:
        tmp.append(aij)
        break
  counter = list(Counter(tmp).items())
  counter.sort(key = lambda x: -x[1])  
  max_kind, max_cnt = counter[0][0], counter[0][1]
  check[max_kind] = 1
  answer.append(max_cnt)
  
print((min(answer)))
# Original solution
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


N, M = list(map(int, input().split()))
mat = [list([int(x)-1 for x in input().split()]) for _ in range(N)]

ans = N
Q = [set([v for v in range(M)])]
while Q:
    sports_cand = Q.pop(0)
    n_sports = [0]*M
    for n in range(N):
        for m in range(M):
            if mat[n][m] in sports_cand:
                n_sports[mat[n][m]] += 1
                break
    max_val = max(n_sports)
    for i, v in enumerate(n_sports):
        if v == max_val:
            ans = min(ans, v)
            if len(sports_cand) > 2:
                Q.append(sports_cand-{i})
print(ans)


# Optimized solution
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


N, M = list(map(int, input().split()))
mat = [list([int(x)-1 for x in input().split()]) for _ in range(N)]

ans = N
Q = [set([v for v in range(M)])]
checked = {"".join([str(v) for v in range(M)])}
while Q:
    sports_cand = Q.pop(0)
    n_sports = [0]*M
    for n in range(N):
        for m in range(M):
            if mat[n][m] in sports_cand:
                n_sports[mat[n][m]] += 1
                break
    max_val = max(n_sports)
    for i, v in enumerate(n_sports):
        if v == max_val:
            ans = min(ans, v)
            next_state = "".join(map(str, sports_cand-{i}))
            if len(sports_cand) > 2 and not next_state in checked:
                Q.append(sports_cand-{i})
                checked.add(next_state)
print(ans)
# Original solution
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


N, M = list(map(int, input().split()))
mat = [list([int(x)-1 for x in input().split()]) for _ in range(N)]

ans = N
sports_cand = set([v for v in range(M)])
for _ in range(M-1):
    C = Counter()
    for n in range(N):
        for m in range(M):
            if mat[n][m] in sports_cand:
                C[mat[n][m]] += 1
                break
    tmp = 0
    key, val = C.most_common(1)[0]
    ans = min(ans, val)
    sports_cand -= {key}
print(ans)


# Optimized solution
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


N, M = list(map(int, input().split()))
mat = [list([int(x)-1 for x in input().split()]) for _ in range(N)]

ans = N
Q = [set([v for v in range(M)])]
while Q:
    C = Counter()
    sports_cand = Q.pop(0)
    for n in range(N):
        for m in range(M):
            if mat[n][m] in sports_cand:
                C[mat[n][m]] += 1
                break
    tmp = 0
    rem_key = set()
    for key, val in C.most_common():
        if val >= tmp:
            tmp = val
            ans = min(ans, val)
            rem_key.add(key)
        else:
            break
    if len(sports_cand) > 2:
        Q.append(sports_cand-rem_key)
print(ans)
# Original solution
from collections import defaultdict

N, M = list(map(int, input().split()))

A = [list(map(int, input().split())) for _ in range(N)]

def search(sportList):
    if not sportList:
        return float('inf')

    cnt = defaultdict(int)

    for p in range(N):
        for a in A[p]:
            if a in sportList:
                cnt[a] += 1
                break

    maxSport = -1
    ret = -float('inf')
    for s, c in list(cnt.items()):
        if ret < c:
            maxSport = s
            ret = c

    sportList.remove(maxSport)
    return min(cnt[maxSport], search(sportList))

sportList = set(range(1, M + 1))
ans = search(sportList)

print(ans)

# Optimized solution
from collections import defaultdict

N, M = list(map(int, input().split()))
INF = float('inf')

A = [tuple(map(int, input().split())) for _ in range(N)]

def search(sports):
    if not sports:
        return INF

    cnt = [0] * (M + 1)
    for a in A:
        for p in a:
            if p in sports:
                cnt[p] += 1
                break

    ret = max(cnt)
    maxSport = cnt.index(ret)
    sports.remove(maxSport)

    return min(ret, search(sports))

ans = search(set(list(range(1, M + 1))))
print(ans)
# Original solution
from collections import Counter

N, M=list(map(int, input().split()))
m=[]
ans = 300

for i in range(N):
    m.append(list(map(int,input().split())))


def search(ln):
    #l = []
    cnt = Counter()
    for r in m:
        for col in r:
            if col in ln:
                cnt[col] += 1
                break
    s,c = cnt.most_common(1)[0]
    global ans
    if c < ans:
        ans = c
    ln.remove(s)
    if len(ln) == 0:
        return 
    search(ln)
search(list(range(1, M+1)))
print (ans)

# Optimized solution
from collections import Counter

N, M=list(map(int, input().split()))
m=[]
ans = 300

for i in range(N):
    m.append(list(map(int,input().split())))

def search(ln):
    #l = []
    cnt = Counter()
    for r in m:
        cnt[r[0]] += 1
    s,c = cnt.most_common(1)[0]
    global ans
    ans = min(ans, c)
    ln.remove(s)
    for r in m:
        r.remove(s)
    if len(ln) == 0:
        return 
    search(ln)
search(list(range(1, M+1)))
print (ans)
# Original solution
import itertools

n, m = input().split(' ');
n = int(n)
m = int(m)
a = [[int(num) for num in input().split(' ')] for _ in range(n)]

sports = [list(itertools.combinations([num + 1 for num in range(m)], i + 1))
          for i in range(m)]

min = 300
for combs in sports:
    for comb in combs:
        dist = {i: 0 for i in comb}
        for player in a:
            for num in player:
                if num in comb:
                    dist[num] += 1
                    break
        max = 0
        for key in dist:
            if dist[key] > max:
                max = dist[key]
        if max < min:
            min = max

print(min)


# Optimized solution
import itertools

n, m = input().split(' ');
n = int(n)
m = int(m)
a = [[int(num) for num in input().split(' ')] for _ in range(n)]

sports = [i + 1 for i in range(m)]

min = 300
for _ in range(m):
    dict = {i: 0 for i in sports}
    for player in a:
        for num in player:
            if num in sports:
                dict[num] += 1
                break
    p = 0
    q = 0
    for i in sports:
        if dict[i] > q:
            p = i
            q = dict[i]
    if q < min:
        min = q
    sports.remove(p)

print(min)
# Original solution
# https://atcoder.jp/contests/agc018/submissions/2152627
# 写経

from collections import deque
from operator import itemgetter


def solve(n, m, prefers):
    def sub(prefers, canceled):
        ret = [0] * m
        for pref in prefers:
            while canceled[pref[0]]:
                pref.popleft()
            ret[pref[0]] += 1
        return max(enumerate(ret), key=itemgetter(1))

    canceled = [False] * m
    ret = n
    for _ in range(m):
        s, c = sub(prefers, canceled)
        canceled[s] = True
        ret = min(ret, c)
    return ret


if __name__ == '__main__':
    n, m = list(map(int, input().split()))
    prefers = [deque(int(x) - 1 for x in input().split()) for _ in range(n)]
    print((solve(n, m, prefers)))


# Optimized solution
# 同じ解法
# 自力で書けるか確認

def solve():
    from operator import itemgetter

    n, m = list(map(int, input().split()))
    a = tuple([int(x) - 1 for x in reversed(input().split())] for _ in range(n))
    excluded = [False] * m

    ret = n
    for _ in range(m):
        cnt = [0] * m
        for p in a:
            while excluded[p[-1]]:
                p.pop()
            cnt[p[-1]] += 1
        j, c = max(enumerate(cnt), key=itemgetter(1))
        ret = min(ret, c)
        excluded[j] = True
    return ret


print((solve()))
# Original solution
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = MAP()
A = list2d(N, M, 0)
for i in range(N):
    for j, a in enumerate(LIST()):
        # データの持ち方を変える
        A[i][a-1] = j

k = 0
valid = [1] * M
ans = INF
while k < M:
    cnt = [0] * M
    for i in range(N):
        mn = INF
        idx = -1
        for j in range(M):
            if valid[j] and A[i][j] < mn:
                mn = A[i][j]
                idx = j
        # 一番順位が高いスポーツにカウント追加
        cnt[idx] += 1
    mx = max(cnt)
    ans = min(ans, mx)
    for i in range(M):
        # 票が最も集まったものは次から選ばないようにする
        if cnt[i] == mx:
            valid[i] = 0
            k += 1
print(ans)


# Optimized solution
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = MAP()
A = [None] * N
for i in range(N):
    A[i] = [a-1 for a in LIST()]

k = 0
valid = [1] * M
ans = INF
while k < M:
    cnt = [0] * M
    for i in range(N):
        mn = INF
        idx = -1
        for j in range(M):
            a = A[i][j]
            if valid[a]:
                # まだ有効なもののうち、一番順位が高いスポーツにカウント追加
                cnt[a] += 1
                break
    mx = max(cnt)
    ans = min(ans, mx)
    for i in range(M):
        # 票が最も集まったものは次から選ばないようにする
        if cnt[i] == mx:
            valid[i] = 0
            k += 1
print(ans)
# Original solution
import sys
input = sys.stdin.buffer.readline

n, m = list(map(int, input().split()))
a = [list(map(int, input().split())) for _ in range(n)]
cnt = [0] * (m + 1)
cur = [0] * n
done = [0] * (m + 1)
for i in range(n):
	cnt[a[i][0]] += 1
	cur[i] = a[i][0]
ans = max(cnt)
for _ in range(m - 1):
	col = cnt.index(max(cnt))
	done[col] = 1
	for i in range(n):
		if cur[i] == col:
			j = 0
			while done[a[i][j]]:
				j += 1
			t = a[i][j]
			cur[i] = t
			cnt[t] += 1
	cnt[col] = 0
	ans = min(ans, max(cnt))
print(ans)


# Optimized solution
import sys
input = sys.stdin.buffer.readline

n, m = list(map(int, input().split()))
a = [list(map(int, input().split())) for _ in range(n)]
cnt = [0] * (m + 1)
cur = [0] * n
cur_idx = [0] * n
done = [0] * (m + 1)
for i in range(n):
	cnt[a[i][0]] += 1
	cur[i] = a[i][0]
ans = max(cnt)
for _ in range(m - 1):
	col = cnt.index(max(cnt))
	done[col] = 1
	for i in range(n):
		if cur[i] == col:
			j = cur_idx[i]
			while done[a[i][j]]:
				j += 1
			t = a[i][j]
			cur[i] = t
			cur_idx[i] = j
			cnt[t] += 1
	cnt[col] = 0
	ans = min(ans, max(cnt))
print(ans)
# Original solution
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    N,M = LI()
    A = [LI() for _ in range(N)]

    ctr = [0] * N

    sp = [0] * (M+1)
    for i in range(N):
        sp[A[i][ctr[i]]] += 1
    ans = max(sp)

    for _ in range(M-1):
        m = 0
        mi = -1
        for i in range(1,M+1):
            if sp[i] > m:
                m = sp[i]
                mi = i
        sp[mi] = -1
        for i in range(N):
            if A[i][ctr[i]] == mi:
                while sp[A[i][ctr[i]]] < 0:
                    ctr[i] += 1
                sp[A[i][ctr[i]]] += 1
        ans = min(ans,max(sp))

    print(ans)


if __name__ == '__main__':
    main()

# Optimized solution
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    N,M = LI()
    A = [LI() for _ in range(N)]

    ctr = [0] * N

    sp = [0] * (M+1)
    for i in range(N):
        sp[A[i][ctr[i]]] += 1
    ans = max(sp)

    for _ in range(M-1):
        mi = sp.index(max(sp))
        sp[mi] = -1
        for i in range(N):
            if A[i][ctr[i]] == mi:
                while sp[A[i][ctr[i]]] < 0:
                    ctr[i] += 1
                sp[A[i][ctr[i]]] += 1
        ans = min(ans,max(sp))

    print(ans)


if __name__ == '__main__':
    main()
# Original solution
import sys
input=sys.stdin.readline
from collections import Counter
n,m=list(map(int,input().split()))
A,B=[],[]
for _ in range(n):
    A.append(tuple([int(x)-1 for x in input().split()]))
    B.append(A[-1][0])
delete=[False]*m
ans=n
for _ in range(m-1):
    c=Counter(B)
    sport,num=c.most_common()[0]
    ans=min(ans,num)
    delete[sport]=True
    for i in range(n):
        idx=0
        while delete[A[i][idx]]:
            idx+=1
        B[i]=A[i][idx]
print(ans)

# Optimized solution
import sys
input=sys.stdin.readline
from collections import Counter
n,m=list(map(int,input().split()))
A,B=[],[]
for _ in range(n):
    A.append(tuple([int(x)-1 for x in input().split()]))
    B.append(A[-1][0])
delete=[False]*m
ans=n
idx=[0]*n
for _ in range(m-1):
    c=Counter(B)
    sport,num=c.most_common()[0]
    ans=min(ans,num)
    delete[sport]=True
    for i in range(n):
        while delete[A[i][idx[i]]]:
            idx[i]+=1
        B[i]=A[i][idx[i]]
print(ans)
# Original solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
total=[0]*(n+1)
asum=[0]*(n+1)
for i in range(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 range(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)


# Optimized solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
s1=[0]*n
s1[0]=a[0]
s2=[0]*n
if a[0]>0:
    s2[0]=a[0]
for i in range(1,n):
    s1[i]+=s1[i-1]+a[i]
    s2[i]+=s2[i-1]
    if a[i]>0:
        s2[i]+=a[i]
ans=-float("inf")
for i in range(n-k+1):
    if i==0:
        tmp1=s1[i+k-1]
        tmp2=s2[n-1]-s2[i+k-1]
    else:
        tmp1=s1[i+k-1]-s1[i-1]
        tmp2=s2[n-1]-(s2[i+k-1]-s2[i-1])
    ans=max(ans,tmp1+tmp2,tmp2)
print(ans)
# Original solution
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
al = [0]*(n+1)
pl = [0]*(n+1)
ans = -10**10
for i in range(n):
  al[i+1] = al[i] + a[i]
  pl[i+1] = pl[i] + max(0,a[i])
for i in range(n-k+1):
  l,r = i,i+k-1
  cur = max(0,al[r+1]-al[l]) + pl[l] + pl[-1] - pl[r+1]
  if cur > ans:
    ans = cur
print(ans)

# Optimized solution
n,k = list(map(int,input().split()))
a = list(map(int, input().split()))
al = [0]*(n+1)
pl = [0]*(n+1)
ans = 0
for i in range(n):
  al[i+1] = al[i] + a[i]
  pl[i+1] = pl[i]
  if a[i] > 0: pl[i+1] += a[i]
for r in range(k-1,n):
  l = r-k+1
  cur = pl[l] + max(0, al[r+1] - al[l]) + pl[n] - pl[r+1]
  if ans < cur: ans = cur
print(ans)
# Original solution
n,k = list(map(int,input().split()))
a = list(map(int, input().split()))
al = [0]*(n+1)
pl = [0]*(n+1)
ans = 0
for i in range(n):
  al[i+1] = al[i] + a[i]
  pl[i+1] = pl[i]
  if a[i] > 0: pl[i+1] += a[i]
for r in range(k-1,n):
  l = r-k+1
  cur = pl[l] + max(0, al[r+1] - al[l]) + pl[n] - pl[r+1]
  if ans < cur: ans = cur
print(ans)

# Optimized solution
n,k = list(map(int,input().split()))
a = list(map(int, input().split()))
al = [0]*(n+1)
pl = [0]*(n+1)
ans = 0
for i in range(n):
  al[i+1] = al[i] + a[i]
  pl[i+1] = pl[i]
  if a[i] > 0: pl[i+1] += a[i]
for r in range(k,n+1):
  l = r-k
  cur = pl[l] + max(0, al[r] - al[l]) + pl[n] - pl[r]
  if ans < cur: ans = cur
print(ans)
# Original solution
from itertools import accumulate

N, K, *A = list(map(int, open(0).read().split()))

M = [a if a < 0 else 0 for a in A]
P = [a if a > 0 else 0 for a in A]

MA = [0] + list(accumulate(M))
PA = [0] + list(accumulate(P))

minus = max(q - p for p, q in zip(MA, MA[K:]))
plus = min(q - p for p, q in zip(PA, PA[K:]))

print((sum(P) + max(minus, -plus)))

# Optimized solution
from itertools import accumulate

def main():
    N, K, *A = list(map(int, open(0).read().split()))

    M = [a if a < 0 else 0 for a in A]
    P = [a if a > 0 else 0 for a in A]

    MA = [0] + list(accumulate(M))
    PA = [0] + list(accumulate(P))

    minus = max(q - p for p, q in zip(MA, MA[K:]))
    plus = min(q - p for p, q in zip(PA, PA[K:]))

    print((sum(P) + max(minus, -plus)))


if __name__ == '__main__':
    main()
# Original solution
n,k = list(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]:
    if nu >= 0:cou += nu
    else:co -= nu
  if cou >= co:ans = max(ans,num-co)
  else:ans = max(ans,num-cou)
print(ans)

# Optimized solution
n,k = list(map(int,input().split()))
lis = list(map(int,input().split()))
num = 0
ans = 0
cou = 0
co = 0
for i in range(n):num += max(0,lis[i])
li = [0 for i in range(n-k+1)]
for i in range(k-1):
  cou += max(0,lis[i])
  co += max(0,-lis[i])
for i in range(n-k+1):
  cou += max(0,lis[i+k-1])
  co += max(0,-lis[i+k-1])
  if cou >= co:ans = max(ans,num-co)
  else:ans = max(ans,num-cou)
  cou -= max(0,lis[i])
  co -= max(0,-lis[i])
print(ans)
# Original solution
from itertools import accumulate
n,k,*a=list(map(int,open(0).read().split()))
p=[x if x>0 else 0 for x in a]
acc=[0]+list(accumulate(a))
p_acc=[0]+list(accumulate(p))
rev_p_acc=[0]+list(accumulate(p[::-1]))
print((max(max(0,acc[k+i]-acc[i])+p_acc[i]+rev_p_acc[n-k-i] for i in range(n-k+1))))

# Optimized solution
from itertools import accumulate
def main():
	n, k, *a = list(map(int, open(0).read().split()))
	p = [x if x > 0 else 0 for x in a]
	acc = [0] + list(accumulate(a))
	p_acc = [0] + list(accumulate(p))
	rev_p_acc = [0] + list(accumulate(p[::-1]))
	print((max(max(0, acc[k + i] - acc[i]) + p_acc[i] + rev_p_acc[n - k - i] for i in range(n - k + 1))))
	
if __name__=="__main__":
	main()
# Original solution
N,K = list(map(int,input().split()))
src = list(map(int,input().split()))

cums = [0]
cump = [0]
for a in src:
    cums.append(cums[-1] + a)
    cump.append(cump[-1] + max(0,a))

ans = 0
for i in range(N-K+1):
    tmp = cump[i]
    tmp += max(0, cums[i+K] - cums[i])
    tmp += cump[N] - cump[i+K]
    ans = max(tmp, ans)
print(ans)


# Optimized solution
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))

cums = [0]
cums_p = [0]
for a in A:
    cums.append(cums[-1] + a)
    cums_p.append(cums_p[-1] + max(0,a))

ans = -float('inf')
for l in range(N-K+1):
    free = cums_p[l] + cums_p[-1] - cums_p[l+K]
    cont = cums[l+K] - cums[l]
    ans = max(ans, free, free+cont)
print(ans)