Harryxun/stf_run huggingface dataset:
n,k=map(int,input().split())
ls,lm,rs,rm=[0],[[0 for i in range(k+1)]for i in range(n+1)],[0],[[0 for i in range(k+1)]for i in range(n+1)]
v=list(map(int,input().split()))
ld=[]
rd=[]
for i in range(n):
    ls.append(ls[-1]+v[i])
    if v[i]<0:
        ld.append(v[i])
        ld=sorted(ld)
    for j in range(1,k+1):
        if j-1<len(ld):
            lm[i+1][j]=lm[i+1][j-1]+ld[j-1]
        else:
            lm[i+1][j]=lm[i+1][j-1]
    ii=n-i-1
    rs.append(rs[-1]+v[ii])
    if v[ii]<0:
        rd.append(v[ii])
        rd=sorted(rd)
    for j in range(1,k+1):
        if j-1<len(rd):
            rm[i+1][j]=rm[i+1][j-1]+rd[j-1]
        else:
            rm[i+1][j]=rm[i+1][j-1]


mx=-9999999999
for x in range(k+1):
    for y in range(x,k+1):
        for z in range(y,k+1):
            for w in range(z,k+1):
                a,b,c,d=x,y-x,z-y,w-z
                if a>=b and c>=d and len(lm)>b and len(rm)>d and ls[a]-lm[a][b]+rs[c]-rm[c][d]>mx:
                    # print(a,b,c,d,ls[a],lm[a][b],rs[c],rm[c][d],ls[a]+lm[a][b]+rs[c]+rm[c][d])
                    mx=ls[a]-lm[a][b]+rs[c]-rm[c][d]
print(mx)


### FIXED CODE ###

n,k=map(int,input().split())
ls,lm,rs,rm=[0],[[0 for i in range(k+1)]for i in range(n+1)],[0],[[0 for i in range(k+1)]for i in range(n+1)]
v=list(map(int,input().split()))
ld=[]
rd=[]
for i in range(n):
    ls.append(ls[-1]+v[i])
    if v[i]<0:
        ld.append(v[i])
        ld=sorted(ld)
    for j in range(1,k+1):
        if j-1<len(ld):
            lm[i+1][j]=lm[i+1][j-1]+ld[j-1]
        else:
            lm[i+1][j]=lm[i+1][j-1]
    ii=n-i-1
    rs.append(rs[-1]+v[ii])
    if v[ii]<0:
        rd.append(v[ii])
        rd=sorted(rd)
    for j in range(1,k+1):
        if j-1<len(rd):
            rm[i+1][j]=rm[i+1][j-1]+rd[j-1]
        else:
            rm[i+1][j]=rm[i+1][j-1]


mx=-999999999
for w in range(k+1):
    for x in range(w+1):
        for y in range(x,w+1):
            for z in range(y,w+1):
                    a,b,c,d=x,y-x,z-y,w-z
                    if a+c<=n and len(ls)>a>=b and len(rs)>c>=d and ls[a]-lm[a][b]+rs[c]-rm[c][d]>mx:
                        # print(a,b,c,d,ls[a],lm[a][b],rs[c],rm[c][d],ls[a]-lm[a][b]+rs[c]-rm[c][d])
                        mx=ls[a]-lm[a][b]+rs[c]-rm[c][d]
print(mx)
n = input()
k = input()
s = input()

word = s[k-1].lower()

b = s[:k-1]

a = s[k:]

new_s = b + word + a

print(new_s)


### FIXED CODE ###


n, k = map(int, input().split())
#k = input()
s = input()

word = s[k-1].lower()

b = s[:k-1]

a = s[k:]

new_s = b + word + a

print(new_s)
N, X = map(int, input().split())
X_list = list(map(int, input().split()))
X_list.append(X)
X_list.sort()
kyori_list = []
for i, x in enumerate(X_list):
    if i == len(X_list) - 1:
        break
    kyori_list.append(X_list[i + 1] - x)

import math
from functools import reduce

def gcd_list(*numbers):
    return reduce(math.gcd, numbers)


print(gcd_list(*kyori_list))
# print(X_list)

### FIXED CODE ###

N, X = map(int, input().split())
X_list = list(map(int, input().split()))
X_list.append(X)
X_list.sort()
kyori_list = []
for i, x in enumerate(X_list):
    if i == len(X_list) - 1:
        break
    kyori_list.append(X_list[i + 1] - x)

import fractions
from functools import reduce


def gcd_list(*numbers):
    return reduce(fractions.gcd, numbers)


print(gcd_list(*kyori_list))
s= input()
ss ="CODEFESTIVAL2016"
t = 1
for i in range(ss):
  if s[i]!=ss[i]:
    t +=1
print(t)

### FIXED CODE ###

s= input()
ss ="CODEFESTIVAL2016"
t = 0
for i in range(len(ss)):
  if s[i]!=ss[i]:
    t +=1
print(t)
import sys
sys.setrecursionlimit(10**6)
def grun(A, K):
    if A < K:
        return 0
    if A%K == 0:
        return A//K
    return grun(A - A//K - 1, K)
def solve():
    N = int(input())
    X = 0
    for _ in range(N):
        A, K = map(int, input().split())
        X ^= grun(A, K)
    print('Takahashi' if X else 'Aoki')

if __name__ == '__main__':
    solve()

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**6)
def grun(A, K):
    if A < K:
        return 0
    a, b = divmod(A, K)
    if b == 0:
        return a
    a += 1
    r = A % a
    return grun((((a-1)*K-r)//a)*a+r, K)
def solve():
    N = int(input())
    X = 0
    for _ in range(N):
        A, K = map(int, input().split())
        X ^= grun(A, K)
    print('Takahashi' if X else 'Aoki')

if __name__ == '__main__':
    solve()
import sys

def main():
    n, k = map(int, sys.stdin.buffer.readline().split())
    a = [0] + list(map(int, sys.stdin.buffer.readline().split()))

    visited = [0]*(n+1)
    town = 1
    for i in range(k+1):
        town = a[town]
        if visited[town] == 0:
            visited[town] = i
        else:
            print(visited.index( (k - visited[town])%(i + visited[town]) + visited[town]))
            exit()
    print(town)

if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys

def main():
    n, k = map(int, sys.stdin.buffer.readline().split())
    a = [0] + list(map(int, sys.stdin.buffer.readline().split()))

    visited = [0]*(n+1)
    town = 1
    for i in range(1, k+1):
        town = a[town]
        if visited[town] == 0:
            visited[town] = i
        else:
            cyc = (k - visited[town])%(i - visited[town]) + visited[town]
            print(visited.index(cyc))
            exit()
    print(town)

if __name__ == "__main__":
    main()
n,m = map(int,input().split())
li = [int(input()) for i in range(m)]

#連続する壊れていない階段の数を求める
li_new = [-1]*(m+1)
li_new[0] = li[0]
for i in range(1,m):
    li_new[i] = li[i] - (li[i-1]+1)
li_new[m] = n - li[m-1]
#print(li_new)

#必用な部分までのフィボナッチ数をリストに保存
fib = [0] * (max(max(li_new)+1,10))
fib[0] = 0
fib[1] = 1
fib[2] = 1
for i in range(3,max(li_new)+1):
    fib[i] = fib[i-2] + fib[i-1]
#print(fib)

#連続する壊れていない階段の数ごとのフィボナッチ数の積
ans = 1
for i in li_new:
    ans *= fib[i]
print(ans%1000000007)

### FIXED CODE ###

n,m = map(int,input().split())
if m > 0:
    li = [int(input()) for i in range(m)]
else:
    li = [0]

#連続する壊れていない階段の数を求める
li_new = [-1]*(m+1)
li_new[0] = li[0]
for i in range(1,m):
    li_new[i] = li[i] - (li[i-1]+1)
li_new[m] = n - li[m-1]
#print(li_new)

#必用な部分までのフィボナッチ数をリストに保存
fib = [0] * (max(max(li_new)+2,3))
fib[0] = 0
fib[1] = 1
fib[2] = 1
for i in range(3,max(li_new)+2):
    fib[i] = fib[i-2] + fib[i-1]
#print(fib)

#連続する壊れていない階段の数ごとのフィボナッチ数の積
ans = 1
if m > 0:
    for i in li_new:
        ans *= fib[i]
else:
    ans = fib[n+1]
print(ans%1000000007)
# Picking Up
# グラフ,有向グラフ,ベクトル, ２次元、コスト,

N = int(input())
S = []
for _ in range(N):
    x, y = map(int, input().split())
    S.append([x, y])
cost = N
d = {}
for l,le in enumerate(S):
    for m, me in enumerate(S):
        if l == m:
            continue
        t = (me[0] - le[0], me[1] - le[1])
        if t in d:
            d[t] +=1
        else:
            d[t] = 1

print(N - max(d.values()))


### FIXED CODE ###

# Picking Up
# グラフ,有向グラフ,ベクトル, ２次元、コスト,

N = int(input())
S = []
for _ in range(N):
    x, y = map(int, input().split())
    S.append([x, y])
cost = N
d = {}

if N==1:
    print(1)
    exit()
for l,le in enumerate(S):
    for m, me in enumerate(S):
        if l == m:
            continue
        t = (me[0] - le[0], me[1] - le[1])
        if t in d:
            d[t] +=1
        else:
            d[t] = 1

print(N - max(d.values()))
N,M = map(int,input().split())
A = map(int,input().split())
new_A = []
for i in range(N):
  if A[a] >= 1/(4*M):
    new_A.append(a)
 
if len(new_A)>=M:
  print("Yes")
else:
  print("No")

### FIXED CODE ###

N,M = map(int,input().split())
A = list(map(int,input().split()))
new_A = []
for i in range(N):
  if A[i] >= sum(A)/(4*M):
    new_A.append(A[i])

if len(new_A)>=M:
  print("Yes")
else:
  print("No")
class unionFind:
    parent = []
    count = 0

    def __init__(self, N):
        self.parent = [i for i in range(N)]
        self.count = N
        self.nums = [1]*N

    def root(self, x):
        if self.parent[x] == x:
            return(x)
        else:
            self.parent[x] = self.root(self.parent[x])
            return(self.parent[x])

    def same(self, x, y):
        x, y = x-1, y-1
        return(self.root(x) == self.root(y))

    def unite(self, x, y):
        x, y = x-1, y-1
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            self.count -= 1
            self.parent[x] = y
            self.nums[y] += self.nums[x]
            self.nums[x] = 0
            return


N, M = map(int, input().split())
G = unionFind(N)
ans = [0]*M
ab = [tuple(map(int, input().split())) for i in range(M)]

tmp = (N * (N-1)) // 2
for i, x in enumerate(reversed(ab)):
    a, b = x[0], x[1]
    ans[M-1-i] = tmp
    if G.same(a, b):
        continue
    else:
        tmp -= G.nums[G.root(a-1)] * G.nums[G.root(b-1)]
        G.unite(a, b)


for i in range(M):
    print(ans[i])


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**5)


class unionFind:
    parent = []
    nums = []

    def __init__(self, n):
        self.parent = [i for i in range(n)]
        self.nums = [1]*n

    def root(self, x):
        if self.parent[x] == x:
            return(x)
        else:
            self.parent[x] = self.root(self.parent[x])
            return(self.parent[x])

    def same(self, x, y):
        x, y = x-1, y-1
        return(self.root(x) == self.root(y))

    def unite(self, x, y):
        x, y = x-1, y-1
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            self.parent[x] = y
            self.nums[y] += self.nums[x]
            self.nums[x] = 0
            return


N, M = map(int, input().split())
G = unionFind(N)
ans = [0]*M
ab = [tuple(map(int, input().split())) for i in range(M)]

tmp = (N * (N-1)) // 2
for i, x in enumerate(reversed(ab)):
    a, b = x[0], x[1]
    ans[M-1-i] = tmp
    a = G.root(a-1)
    b = G.root(b-1)
    if a == b:
        continue
    else:
        tmp -= G.nums[a] * G.nums[b]
        G.parent[a] = b
        G.nums[b] += G.nums[a]
        G.nums[a] = 0


for i in range(M):
    print(ans[i])
a = raw_input()
x,y = a.split(" ")
n = int(a[0])
m = int(a[1])
S = n*m
SUM = n * 2 + m * 2
print S + " " + SUM

### FIXED CODE ###

x = raw_input().split(" ")
 
a = int(x[0])
b = int(x[1])
 
print str(a*b) + " " + str(2*(a+b))
# -*- coding: utf-8 -*-


def main():
    a, b = [int(e) for e in input().split()]
    answer = (a + b) % 23
    return answer


print((main())


### FIXED CODE ###

# -*- coding: utf-8 -*-


def main():
    a, b = [int(e) for e in input().split()]
    answer = (a + b) % 24
    return answer


print(main())
import heapq

n,m=map(int,input().split())
X=[[] for _ in range(m)]
for i in range(n):
    a,b=map(int,input().split())
    X[a-1].append(-b)

ans=0
p=[]
heapq.heapify(p)
for i in range(m):
    if len(p)==0 and len(X[i])==0:
        continue
    for j in range(len(X[i])):
        heapq.heappush(p,X[i][j])
    a=heapq.heappop(p)
    ans+=a*-1
print(ans)

### FIXED CODE ###

import heapq

n,m=map(int,input().split())
X=[[] for _ in range(10**5+5)]
for i in range(n):
    a,b=map(int,input().split())
    X[a-1].append(-b)

ans=0
p=[]
heapq.heapify(p)
for i in range(m):
    if len(p)==0 and len(X[i])==0:
        continue
    for j in range(len(X[i])):
        heapq.heappush(p,X[i][j])
    a=heapq.heappop(p)
    ans+=a*-1
print(ans)
import sys

sys.setrecursionlimit(100000)

MOD = 1000000007


def step_state(n, broken):
    if n == 0:
        return 1
    elif n == 1:
        if broken[1]:
            return 0
        else:
            return 1
    else:
        if broken[n - 1] and broken[n - 2]:
            return 0
        elif broken[n - 1]:
            return step_state(n-2, broken) % MOD
        elif broken[n-2]:
            return step_state(n-1, broken) % MOD
        else:
            return (step_state(n-1, broken)+step_state(n-2, broken)) % MOD


if __name__ == '__main__':
    n, m = map(int, input().split())
    a = set([int(input()) for i in range(m)])
    broken = [False] * (n+1)
    for i in range(n+1):
        if i in a:
            broken[i] = True

    answer = step_state(n, broken)

    print(answer)


### FIXED CODE ###

if __name__ == '__main__':
   n, m = map(int,input().split())
   a = set([int(input()) for i in range(m)])

   MOD = 1000000007

   dp = [0 for i in range(n+1)]
   dp[0] = 1
   if 1 in a:
      dp[1] = 0
   else:
      dp[1] = 1

   for i in range(2,n+1):
      if (i-1 in a) and (i-2 in a):
         dp[i] = 0
      elif i-1 in a:
         dp[i] = dp[i-2] % MOD
      elif i-2 in a:
         dp[i] = dp[i-1] % MOD
      else:
         dp[i] = (dp[i-1] + dp[i-2]) % MOD

   print(dp[n])
n = int(input())
a = list(map(int,input().split()))

flg = "default" #increase_flg
cnt = 0
for i in range(n-1):
  if a[i+1] > a[i]:
    if not flg:
      cnt+=1
      flg = "default"
    else:
      flg = True
  elif a[i+1] < a[i]:
    if flg:
      cnt+=1
      flg = "default"
    else:
      flg = False
#  print(a[i],a[i+1],flg,cnt)

if not a[i]>=a[i-1]>=a[i-2] or a[i]<=a[i-1]<=a[i-2]:
  cnt +=1 
print(cnt)

### FIXED CODE ###

from collections import deque
 
n = int(input())
a = list(map(int,input().split()))
d = deque(a)
 
tmp = []
cnt = 0
while d:
  v = d.popleft()
  if len(tmp)<=1:
    pass
  else:
    if not (v >= tmp[-1] >= tmp[-2] >= tmp[0] or v <= tmp[-1] <= tmp[-2] <= tmp[0]):
      tmp = []
      cnt += 1
  tmp.append(v)
#  print(d,tmp,cnt)
if tmp:
  cnt+=1
print(cnt)
s=str(input())
x=list(s)
out=""
for  i in len(x):
 if (i+1) % 2 !=1:
  out+=x[i]
print(out)

### FIXED CODE ###

x,t=map(int, input().split())

print(max(x-t,0))
import sys
N = int(input())
lis = [int(i) for i in sys.stdin.readline().split()]
x_1,x_2 = 0,0
for i in range(2,N):
    tmp = min(abs(lis[i]-lis[i-1]) + x_1, abs(lis[i] - lis[i-2]) + x_2)
    x_1,x_2 = tmp,x_1
print(tmp)

### FIXED CODE ###

import sys
N = int(input())
lis = [int(i) for i in sys.stdin.readline().split()]
res = [0,abs(lis[1]-lis[0])]
for i in range(2,N):
    tmp = min(abs(lis[i]-lis[i-1]) + res[i-1],abs(lis[i] - lis[i-2]) + res[i-2])
    res.append(tmp)
print(res[-1])
a, b, k = map(int, input().split())
for i in range(k):
    if a >= 1:
        a-=1
    elif b >= 1:
        b-=1
    else:
        path
print(a, b)

### FIXED CODE ###

a, b, k = map(int, input().split())
if a - k > 0:
    a = a - k
elif a - k < 0:
    if b - (k - a) > 0:
        b = b - (k - a)
        a = 0
    else:
        b = 0
        a = 0
else:
    a = 0
print(a, b)
40
ferelibenterhominesidquodvoluntcredunt

### FIXED CODE ###

n=int(input())
s=input()
if len(s)>n:
  print(s[:n]+"...")
else:
  print(s)
import sys

n = int(input())
sys.setrecursionlimit(n)



def count(num):
    if num > n:
        return 0
    fl = 0
    s = str(num)
    if '3' in s and '5' in s and '7' in s:
        fl = 1

    return fl + count(num*10+3) + count(num*10+5) + count(num*10+7)


print(count(0))

### FIXED CODE ###

import sys

n = int(input())
sys.setrecursionlimit(2*10**9+1)


def count(num):
    if num > n:
        return 0
    fl = 0
    s = str(num)
    if '3' in s and '5' in s and '7' in s:
        fl = 1

    return fl + count(num*10+3) + count(num*10+5) + count(num*10+7)


print(count(0))
N = input()

lastnum = N[-1]

if lastnum in list[0,1,6,8]
print("pon")

elsif lastnum in list[3,]
print("bon")

else
print("hon")

### FIXED CODE ###

def solve(n):
    p = n % 10
    if p in [2,4,5,7,9]:
        return "hon"
    elif p in [0,1,6,8]:
        return "pon"
    else:
        return "bon"

n = int(input())
print(solve(n))
N,M = map(int,input().split())
s = [int(input()) for i in range(M)]
c = [int(input()) for i in range(M)]
l = []
first = []
second = []
third = []
for i in range(M):
  if s[i] == 1:
    first.append(c[i])
  elif s[i] == 2:
    second.append(c[i])
  elif s[i] == 3:
    third.append(c[i])
if first == []:
  print(-1)
elif second == []:
  second.append(0)
elif third == []:
  third.append(0)
l.append(min(first))
l.append(min(second))
l.append(min(third))
l = ''.join(str(n) for n in l)
print(l)



### FIXED CODE ###

N,M = map(int,input().split())
sc=[list(map(int,input().split()))for _ in range(M)]
ans = -1
for i in range(1000):
  s = str(i)
  if len(s) == N:
    flg = 0
    for a,b in sc:
      if int(s[a-1]) != b:
        flg = 1
    if flg == 0:
      ans = i
      break
print(ans)
n = int(raw_input())
for i in range(n):
	marks = map(float, raw_input().split())
	AB = (marks[1] - marks[3]) / (marks[0] - marks[2])
	CD = (marks[5] - marks[7]) / (marks[4] - marks[6])
	if AB == CD:
		print 'YES'
	else:
		print 'NO'

### FIXED CODE ###

for i in range(int(input())):
    x1, y1, x2, y2, x3, y3, x4, y4 = map(float, input().split())
    if abs((y1 - y2) * (x3 - x4) - (y3 - y4) * (x1 - x2)) < 1.e-10:
        print('YES')
    else:
        print('NO')
n = int(input())

a = list(map(int, input().split()))
a.sort()


from functools import lru_cache
import sys
if n == 2:
    print(max(a))
    sys.exit()


@lru_cache(maxsize=100000)
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def gcd_list(li):
    g = gcd(li[0], li[1])
    for i in range(2, len(li)):
        g = gcd(g, a[i])
    return g


def gcd_list_2(li):
    g = gcd(li[0], li[1])
    if g == 1:
        return max(gcd_list([li[0]] + li[2:]), gcd_list(li[1:]))
    for i in range(2, n):
        if gcd(g, a[i]) == 1:
            return gcd_list([g] + li[i + 1:])
        else:
            g = gcd(g, a[i])
    return g


def max_gcd(a):
    g = gcd_list(a)
    a = [a[i]//g for i in range(n)]
    m = gcd_list_2(a)
    print(g * m)


max_gcd(a)

### FIXED CODE ###


n = int(input())

a = list(map(int, input().split()))
a.sort(reverse=True)


from functools import lru_cache
import sys
if n == 2:
    print(max(a))
    sys.exit()


@lru_cache(maxsize=100000)
def gcd(x, y):
    while y:
        x, y = y, x % y
    return x


def gcd_list(li):
    if len(li) == 1:
        return li[0]
    g = gcd(li[0], li[1])
    for i in range(2, len(li)):
        g = gcd(g, li[i])
    return g


def gcd_list_2(li):
    g = gcd(li[0], li[1])
    if g == 1:
        return max(gcd_list([li[0]] + li[2:]), gcd_list(li[1:]))
    for i in range(2, n):
        if gcd(g, li[i]) == 1:
            if i == n - 1:
                return gcd_list(li[:i])
            gg = gcd_list(li[i+1:])
            bb = [gcd(li[x], gg) for x in range(i+1)]
            bb.sort()
            return bb[1]
        else:
            g = gcd(g, li[i])
    return g


def max_gcd(a):
    g = gcd_list(a)
    a = [a[i]//g for i in range(n)]
    m = gcd_list_2(a)
    print(g * m)


max_gcd(a)
import sys

args = sys.argv
r = int(args[1])
g = int(args[2])
b = int(args[3])

mul = r*100 + g*10 + b

if mul % 4 == 0:
    print("YES")
else:
    print("NO")


### FIXED CODE ###

r,g,b = [int(x) for x in input().split()]

mul = r*100 + g*10 + b

if mul % 4 == 0:
    print("YES")
else:
    print("NO")
s = input().replace('/','')
if s[4:5] == 1:
  print('TBD')
else:
  if s[5:6] > 4:
    print('TBD')
  else:
    print('Heisei')


### FIXED CODE ###

s = input().replace('/','')
if s[4:5] == '1':
  print('TBD')
else:
  if int(s[5:6]) > 4:
    print('TBD')
  else:
    print('Heisei')
s = input()
t = input()
lists = list()

for i in range(len(s)-len(t)):
  ans = 0
  for j in range(len(t)-1):
    if s[i+j] == t[j]:
      ans = ans + 1
    lists.append(ans)

print(len(t)-max(lists))

### FIXED CODE ###

s = input()
t = input()
lists = list()

for i in range(len(s)-len(t)+1):
  ans = 0
  for j in range(len(t)):
    if s[i+j] == t[j]:
      ans += 1
    lists.append(ans)

print(len(t)-max(lists))
s = str(input())
k = int(input())
if s[:k] == ["1"]*k:
    ans = "1"
else:
    ans = s[:k].replace("1", "")[0]
print(ans)

### FIXED CODE ###

s = str(input())
k = int(input())
ans = None
for i in range(k):
    if s[i] != "1":
        ans = s[i]
        break
if ans != None:
    print(ans)
else:
    print("1")
import sys
a, b = sys.stdin.readline()
c = a*b
if (c % 2) == 0:
  print("Even")
else:
  print("Odd")

### FIXED CODE ###

a, b = map(int, input().split())
if ((a*b) % 2) == 0:
    print("Even")
else:
    print("Odd")
n,k=map(int,input().split())
v=list(map(int,input().split()))
def get(t,s): # t個のうち前からs個
    return sorted(v[:s]+v[n-(t-s):])

def f(t):
    res=0
    for i in range(t+1):
        a=get(min(t,n),min(i,n))
        tmp=sum(a)
        res=max(res,tmp)
        for j in range(min(k-t,t)):
            tmp-=a[j]
            res=max(res,tmp)
    return res

ans=0
for i in range(1,k+1):
    ans=max(ans,f(i))
print(ans)

### FIXED CODE ###

n,k=map(int,input().split())
v=list(map(int,input().split()))
def get(t,s): # t個のうち前からs個
    return sorted(v[:s]+v[n-(t-s):])

def f(t):
    res=0
    for i in range(t+1):
        a=get(min(t,n),min(i,n))
        tmp=sum(a)
        res=max(res,tmp)
        for j in range(min(k-t,t,n)):
            tmp-=a[j]
            res=max(res,tmp)
    return res

ans=0
for i in range(1,k+1):
    ans=max(ans,f(i))
print(ans)
N = int(input())


class Node:
    def __init__(self, index):
        self.index = index
        self.edges = []

class Edge:
    def __init__(self, index, a, b):
        self.a = a
        self.b = b
        self.index = index
        self.color = 0

    def to(self, fm):
        if fm == self.a:
            return self.b
        elif fm == self.b:
            return self.a
        else:
            return None


nodes = [Node(i) for i in range(N)]
edges = []
for i in range(N-1):
    a, b = map(int, input().split())
    edge = Edge(i, a-1, b-1)
    edges.append(edge)
    nodes[a-1].edges.append(edge)
    nodes[b-1].edges.append(edge)

passed = set()


def rec(node, via_color, K):
    passed.add(node.index)
    color = 1
    for edge in node.edges:
        to_node_index = edge.to(node.index)
        if to_node_index in passed:
            continue
        if color == via_color:
            color += 1
        edge.color = color
        K = rec(nodes[to_node_index], color, K)
        color += 1
    return max(color-1, K)


K = rec(nodes[0], 0, 1)
print(K)
for edge in edges:
    print(edge.color)

### FIXED CODE ###

import sys
sys.setrecursionlimit(1000000000)

N = int(input())


class Node:
    def __init__(self, index):
        self.index = index
        self.edges = []

class Edge:
    def __init__(self, index, a, b):
        self.a = a
        self.b = b
        self.index = index
        self.color = 0

    def to(self, fm):
        if fm == self.a:
            return self.b
        elif fm == self.b:
            return self.a
        else:
            return None


nodes = [Node(i) for i in range(N)]
edges = []
for i in range(N-1):
    a, b = map(int, input().split())
    edge = Edge(i, a-1, b-1)
    edges.append(edge)
    nodes[a-1].edges.append(i)
    nodes[b-1].edges.append(i)

passed = set()


def rec(node, via_color, K):
    passed.add(node.index)
    color = 1
    for edge_index in node.edges:
        edge = edges[edge_index]
        to_node_index = edge.to(node.index)
        if to_node_index in passed:
            continue
        if color == via_color:
            color += 1
        edge.color = color
        K = rec(nodes[to_node_index], color, K)
        color += 1
    return max(color-1, K)


K = rec(nodes[0], 0, 1)
print(K)
for edge in edges:
    print(edge.color)
import heapq

n,m = (int(i) for i in input().split())
jobs = [[] for i in range(m)]
for i in range(n):
    tmp,tmp2 = (int(i) for i in input().split())
    if(tmp>m):
        continue
    jobs[m-tmp].append(tmp2)
    
ans = 0
h=[]
for i in range(m-1,-1,-1):
    for b in jobs[i]:
        heapq.heappush(h,b)
        heapq._heapify_max(h)
        
    if(len(h)!=0):
        ans += heapq._heappop_max(h)
        print(h)

print(ans)

### FIXED CODE ###

import heapq

n,m = (int(i) for i in input().split())
jobs = [[] for i in range(m)]
for i in range(n):
    tmp,tmp2 = (int(i) for i in input().split())
    if(tmp>m):
        continue
    jobs[m-tmp].append(tmp2)
    
ans = 0
h=[]
for i in range(m-1,-1,-1):
    for b in jobs[i]:
        heapq.heappush(h,-b)
        
    if(len(h)!=0):
        a = -heapq.heappop(h)
        ans += a

print(ans)