Harryxun/stf_run huggingface dataset:
def calculate(m,n):
  dp=[[0 for j in range(n)] for i in range(n)]
  best=0
  for i in range(n):
    for j in range(n):
        if i!=j:
            dp[i][j]=max(dp[i][j],dp[i][j]+m[i][j]) 
    best+=max(dp[i])
  return best
 
def main():
    n=int(input())
    m=[[n]]*(n)
    for i in range(n):
        m[i]=list(map(int,input().split())) 
    print(calculate(m,n))
 
if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
    n=int(input())
    dp=[0]*(3)
    for _ in range(n):
        dp1=[0]*3 
        nums=list(map(int,input().split()))
        for i in range(3):
            for j in range(3):
                if i!=j:
                    dp1[j]=max(dp1[j],dp[i]+nums[j])
            
        dp=dp1
    print( max(dp[0],dp[1],dp[2]))
if __name__ == '__main__':
    main()
N,K = map(int,input().split())

    print(N - K + 1)


### FIXED CODE ###

N, K = map(int,input().split())
print(N - K + 1)
#!/usr/bin/env python3

S = input()
N = len(S)
count = 0
is_ok = True

right_index = 0
left_index = N - 1

while right_index != left_index:

    if right_index == left_index:
        break

    if S[right_index] == S[left_index]:
        right_index += 1
        left_index -= 1
        continue
    if S[right_index] == "x":
        count += 1
        right_index += 1
        continue

    if S[left_index] == "x":
        count += 1
        left_index -= 1
        continue

    count = -1
    break

ans = count

print(ans)

### FIXED CODE ###

#!/usr/bin/env python3

S = input()
N = len(S)
count = 0
is_ok = True

right_index = 0
left_index = N - 1

while right_index < left_index:

    if right_index == left_index:
        break

    if S[right_index] == S[left_index]:
        right_index += 1
        left_index -= 1
        continue
    if S[right_index] == "x":
        count += 1
        right_index += 1
        continue

    if S[left_index] == "x":
        count += 1
        left_index -= 1
        continue

    count = -1
    break

ans = count

print(ans)
s = int(input())
num = [[0 for i in range(10)] for j in range(10)]

ans = 0

for _ in range(n):
    i = _ + 1
    if i < 10:
        num[i][i] += 1
    elif i % 10 != 0:
        moji = str(i)
        num[int(moji[0])][int(moji[len(moji)-1])] += 1
    else:
        pass

for i in range(1, 10):
    for j in range(1, 10):
        ans += num[i][j] * num[j][i]

for i in num:
    for j in i:
        print(j, " ", end="")
    print()

print(ans)

### FIXED CODE ###

n = int(input())
num = [[0 for i in range(10)] for j in range(10)]

ans = 0

for _ in range(n):
    i = _ + 1
    if i < 10:
        num[i][i] += 1
    elif i % 10 != 0:
        moji = str(i)
        num[int(moji[0])][int(moji[len(moji)-1])] += 1
    else:
        pass

for i in range(1, 10):
    for j in range(1, 10):
        ans += num[i][j] * num[j][i]

print(ans)
//入力部分以外わからなかった。

S = input()
if S[0] == S[1] == S[2] == :
    print("no")
else:
    print("yes")


### FIXED CODE ###

s = input()
if s == "AAA" or s == "BBB":
    print("No")
else:
    print("Yes")
from math import pi
r = int(input())
S = pi * r * r
R = 2 * pi * r
print("{0} {1}".format(S, R))

### FIXED CODE ###

from math import pi
r = float(input())
S = pi * r * r
R = 2 * pi * r
print("{0} {1}".format(S, R))
a,b,c,x,y = map(int, raw_input().split())
ans = min(a*x + b*y, min(2*c*max(x,y), 2*c*min(x,y) + (x-y)*a if x>y else 2*c*min(x,y) + (y-x)*b))
print(ans)

### FIXED CODE ###

import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

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

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

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 I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    A, B, C, X, Y = LI()
    if X > Y:
        X, Y = Y, X
        A, B = B, A
    money_a_b = X * A + Y * B
    money_b_c = (Y - X) * B + (2 * X) * C
    money_c = Y * 2 * C
    ans = min(money_a_b, money_b_c, money_c)
    print(ans)

main()
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

N=int(input())
datas=prime_factorize(N)
a=1
b=1
for i in range(len(data)):
  if a<b:
    a+=data[i]
  else:
    b+=data[i]

print(max(1,a-1)+max(1,b-1))



### FIXED CODE ###

N=int(input())
import math
SQ=int(math.sqrt(N))
minval=10**12
a=1
b=1
for i in range(1,SQ+1):
  if N%i==0:
    newa=i
    newb=N//i
    if abs(newa-newb) < minval:
      minval=abs(newa-newb)
      a=newa
      b=newb

print(a+b-2)
n=int(input())
s=[[]for i in range(n+1)]
d=[1]
s[int(input())].append(0)
for i in range(1,n):
    j=int(input())
    t=0
    s[j].append(i)
    if len(s[j])>1:
        if s[j][-2]!=s[j][-1]-1:
            t+=d[i-1]+d[s[j][-2]]
        else:
            t+=d[i-1]
    else: 
        t+=d[i-1]
    d.append(t)
print(d[-1]%(10**9+7))   

### FIXED CODE ###

n=int(input())
s=[[]for i in range(2*10**5+1)]
d=[1]
s[int(input())].append(0)
for i in range(1,n):
    j=int(input())
    t=0
    s[j].append(i)
    if len(s[j])>1:
        if s[j][-2]!=s[j][-1]-1:
            t+=d[i-1]+d[s[j][-2]]
        else:
            t+=d[i-1]
    else: 
        t+=d[i-1]
    d.append(t)
print(d[-1]%(10**9+7))
def fib(n):
   if n <= 2:
     return 1
   else :
     return fib(n-1)+fib(n-2)

n,m = map(int,input().split())
a =  [int(input()) for i in range(m)]
a.append(n+1)

time_f=[]

for i,v in enumerate(a):
    if i == 0:
        # print (fib(a[i]))
        time_f = fib(a[i])
    else:
        # print((a[i]-a[i-1]-1))
        # print (fib(a[i]-a[i-1]-1))
        time_f *=fib(a[i]-a[i-1]-1)


print(time_f%1000000007)

### FIXED CODE ###

n,m = map(int,input().split())
a =  [int(input()) for i in range(m)]

dp =[0]*(n+1)

for i in a:
    dp[i] = -1

for i in [0,1]:
    if dp[i] == -1:
        dp[i] =0
    else:
        dp[i] =1

for i in range(2,n+1):
    if dp[i] == -1:
        dp[i] = 0
    else:
        if dp[i-1]==dp[i-2]==0:break
        dp[i] = (dp[i-2]+dp[i-1])

print(dp[-1]%1000000007)
N = input()
A = list(map(int, input().split()))

answer = 1
if 0 in A:
    print(0)
else:
    for i in A:
        answer *= i
        if answer > 10**18:
            print(-1)
            exit()
print(answer)
    


### FIXED CODE ###

a = list(map(int, input().split()))
print(a[0] * a[1])
from collections import Counter
a = int(input())
l = []
i = 1
for i in range(a):
    b = [int(i) for i in input(i).split(" ")]
    l.append(b)
c=Counter(l)
d = dict(c)
kv = []
for k,v in d.items():
    if k>v:
        kv.append(v)
    elif k<v:
        t = v-k
        kv.append(t)
s = sum(kv)
print(s)




### FIXED CODE ###

from collections import Counter
a = int(input())
l = list(map(int, input().split()))
#i = 1

c=Counter(l)
d = dict(c)
kv = []
for k,v in d.items():
    if k>v:
        kv.append(v)
    elif k<v:
        t = v-k
        kv.append(t)
s = sum(kv)
print(s)
s = input()
print(int(s.count(1))

### FIXED CODE ###

S = input()
li = list(S)
 
ans = 0
for i in range(3):
  if li[i] == "1":
    ans += 1
print(ans)
S = str(input())
if S .isupper():
  print(A)
else:
  print(a)

### FIXED CODE ###

S = str(input())
if S.isupper():
  print ("A")
else:
  print ("a")
n,m,k = map(int,input().split())
Max = n*m
#二項係数とその逆元テーブルを作る前処理
fac = [0]*(Max)
finv = [0]*(Max)
inv = [0]*(Max)
fac[0]=fac[1]=1
finv[0]=finv[1]=1
inv[1]=1
for i in range(2,Max):
        fac[i] = fac[i-1] * i % mod
        inv[i] = mod - inv[mod%i]*(mod//i)%mod
        finv[i] = finv[i-1]*inv[i]%mod
#O(1)でmod計算した組合せ数を計算
def C(n,r):
    if n < r:
        return 0
    if n < 0 or r < 0 :
        return 0
    return fac[n]*(finv[r]*finv[n-r]%mod)%mod
#mod計算した順列の計算
def P(n,r):
    if n < r:
        return 0
    if n<0 or r<0:
        return 0
    return (fac[n]*finv[n-r])%mod


c = C(n*m-2,k-2)
res = 0

for i in range(1,n):
    res += i * (n-i) *(m**2)*c
    res %= mod

for i in range(1,m):
    res += i*(m-i)*(n**2)*c
    res %= mod
print(res)

### FIXED CODE ###

from itertools import*
import math
from collections import*
from heapq import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = float("inf")
mod = 10**9+7
from functools import reduce
import sys
sys.setrecursionlimit(10**7)

n,m,k = map(int,input().split())
Max = n*m
#二項係数とその逆元テーブルを作る前処理
fac = [0]*(Max)
finv = [0]*(Max)
inv = [0]*(Max)
fac[0]=fac[1]=1
finv[0]=finv[1]=1
inv[1]=1
for i in range(2,Max):
        fac[i] = fac[i-1] * i % mod
        inv[i] = mod - inv[mod%i]*(mod//i)%mod
        finv[i] = finv[i-1]*inv[i]%mod
#O(1)でmod計算した組合せ数を計算
def C(n,r):
    if n < r:
        return 0
    if n < 0 or r < 0 :
        return 0
    return fac[n]*(finv[r]*finv[n-r]%mod)%mod
#mod計算した順列の計算
def P(n,r):
    if n < r:
        return 0
    if n<0 or r<0:
        return 0
    return (fac[n]*finv[n-r])%mod


c = C(n*m-2,k-2)
res = 0

for i in range(1,n):
    res += i * (n-i) *(m**2)*c
    res %= mod

for i in range(1,m):
    res += i*(m-i)*(n**2)*c
    res %= mod
print(res)
h,n = map(int,input().split())
a = list(map(int,input().split()))
p = sum(a)
print("Yes" if h < =p else "No")

### FIXED CODE ###

h,n = map(int,input().split())
a = list(map(int,input().split()))
p = sum(a)
print("Yes" if h <= p else "No")
# C - Bugged
# DP Overkill

N = int(input())
s = [int(input()) for _ in range(N)]

dp = [0] + [-1]*sum(s)

while s:
    x = s.pop()
    for idx in reversed(range(len(dp))):
        if dp[idx] != -1 and idx + x <= len(dp) - 1:
            dp[idx+x] = dp[idx] + x
            
dp = set(dp)
dp.remove(-1)
dp = list(dp)
dp.sort(reverse = True)

for idx in range(len(dp)):
    if idx == len(dp) - 1:
        print(0)
    elif dp[idx] % 10 > 0:
        print(dp[idx])
        break

### FIXED CODE ###

# C - Bugged

N = int(input())
s = [int(input()) for _ in range(N)]

mod10 = [x%10 for x in s]
fraction = [x for x in s if x%10 > 0]

if len(set(mod10)) == 1 and mod10[0] == 0:
    print(0)
elif sum(mod10)%10 > 0:
    print(sum(s))
else:
    print(sum(s) - min(fraction))
a = int(input())
blue = []
count = []
for i in range(a):
    x = input()
    if x in blue:
        count[blue.index(x)] += 1
    else:
        blue.append(x)
        count.append(1)
b = int(input())
for i in range(b):
    x = input()
    if x in blue:
        count[blue.index(x)] -= 1
    else:
        blue.append(x)
        count.append(-1)

count.sort()
count.reverse()
if count[0] < 0:
    print("0")
else:
    print (count[0])

### FIXED CODE ###

a,b,c = [int(i) for i in input().split()]
if a + b >= c:
    print("Yes")
else:
    print("No")
n, k = map(int, input().split())
a = list(map(int, input().split()))

p_list = [1]
ne_p = a[0]
for _ in range(k):
    p_list.append(ne_p)
    ne_p = a[ne_p-1]
    if ne_p in p_list:
        ind = p_list.index(ne_p)
        break
k = k - ind
print(p_list[ind + k%(len(p_list)-ind)])

### FIXED CODE ###

import sys 
def solve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    p_bool = [False] * n
    ne_p = 1
    p_bool[ne_p-1] = True
    step = 0
    for i in range(n):
        ne_p = a[ne_p-1]
        step += 1
        if p_bool[ne_p-1]:
            break
        else:
            p_bool[ne_p-1] = True
        if step == k:
            print(ne_p)
            sys.exit()
    
    p_list = []
    sta = ne_p
    for i in range(n):
        ne_p = a[ne_p-1]
        step += 1
        p_list.append(ne_p)
        if ne_p == sta:
            break
        if step == k:
            print(ne_p)
            sys.exit()

    len_l = len(p_list)
    left = k-step
    left -= (left//len_l) * len_l
    print(p_list[left-1])
    # p_list = [1]
    # ne_p = a[0]
    # p_bool = [False] * n
    # p_bool[0] = True
    # check = False
    # step = 0
    # for _ in range(n):
    #     p_bool[ne_p-1] = True
    #     p_list.append(ne_p)
    #     step = 0
    #     if step == k:
    #         #print(ne_p)
    #         sys.exit()
    #     ne_p = a[ne_p-1]
    #     # if ne_p in p_list:
    #     #     ind = p_list.index(ne_p)
    #     #     break
    #     if p_bool[ne_p-1]:
    #         check = True
    #         break
        
    # #print(p_list)
    # #print(p_bool)
    # if check:
    #     ind = p_list.index(ne_p)
    # else:
    #     ind = 0
    # k = k - ind
    # #print(ind)
    # #print(k)
    # #print(p_list)
    # #print(k%(len(p_list)-ind))
    # print(p_list[ind + k%(len(p_list)-ind)])

solve()
n,k=map(int,input().split())
x=list(map(int,input().split()))
from bisect import bisect_left as bl

x0=bl(x,0)
if x[x0]==0:
    k-=1
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0+1:]
else:
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0:]
#print(a,b)
ans=float("INF")
if k==0:
    print(0)
    exit()
if k-1<len(a):
    ans=min(ans,a[k-1])
if k-1<len(b):
    ans=min(ans,b[k-1])
for i in range(k):
    j=k-i-2
    if i<len(a) and 0<=j and j<len(b):
        ans2=2*a[i]+b[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
for i in range(k):
    j=k-i-2
    if i<len(b) and 0<=j and j<len(a):
        ans2=b[i]+2*a[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
print(ans)


### FIXED CODE ###

n,k=map(int,input().split())
x=[0]
for xi in input().split():
    xi=int(xi)
    if xi==0:
        k-=1
    else:
        x.append(xi)
x.sort()
ans=float("INF")
zero=x.index(0)
for i in range(max(0,zero-k-1),min(zero+1,n-k+1)):
    if x[i+k]<0 or x[i]>0:
        continue
    jogen=abs(x[i+k])
    kagen=abs(x[i])
    ans=min(ans,jogen*2+kagen,kagen*2+jogen)
print(ans)
"""
# Too slow
def add2(a, b):
    a, b = align_digit(a, b)
    zipped = zip(a[::-1], b[::-1])
    carry = 0
    result = []
    for a, b in zipped:
        s = int(a) + int(b) + carry
        carry = int(s / 10)
        result.append(str(s % 10))
    if result[-1] == '0':
        result[-1] = str(carry)
    print(''.join(result[::-1]))
"""


def align_digit(a, b):
    la, lb = len(a), len(b)
    if la < lb:
        a = a.zfill(lb)
    elif lb < la:
        b = b.zfill(la)
    return a, b


def split_to_ints(s, n=10):
    length = len(s)
    return [int(s[i-n:i]) for i in range(length, 0, -n)]


def add(a, b, n=10):
    a, b = align_digit(a, b)
    print(a, b)
    ints_a = split_to_ints(a, n)
    ints_b = split_to_ints(b, n)
    carry = 0
    result = []

    zipped = list(zip(ints_a, ints_b))[::-1]
    print(zipped)
    for i, (int_a, int_b) in enumerate(zipped):
        s = int_a + int_b + carry
        carry = int(s / (10 ** n))
        if i != len(zipped) - 1:
            result.append(str(s % (10 ** (n - 1))).zfill(n))
        else:
            result.append(str(s))

    print(result)
    ret = str(int(''.join(reversed(result))))
    if len(ret) > 80:
        return 'overflow'
    return ret


if __name__ == '__main__':
    n_input = int(input())
    for i in range(n_input):
        a, b = input(), input()
        print(add(a, b))


### FIXED CODE ###

# 12,432 -> 012,432
def align_digits(a, b):
    la, lb = len(a), len(b)
    if la < lb:
        a = a.zfill(lb)
    elif lb < la:
        b = b.zfill(la)
    return a, b


def add(a, b):
    a, b = align_digits(a, b)
    zipped = list(zip(a[::-1], b[::-1]))
    carry = 0
    result = []
    for i, z in enumerate(zipped):
        a, b = z
        s = int(a) + int(b) + carry
        carry = int(s / 10)
        if i == len(zipped) - 1:
            result.append(str(s))
        else:
            result.append(str(s % 10))
    ans = ''.join(result[::-1])
    if len(ans) > 80:
        return 'overflow'
    return ans


if __name__ == '__main__':
    n_input = int(input())
    inputs = []
    for i in range(n_input):
        inputs.append((input(), input()))
    for a, b in inputs:
        print(add(a, b))
c=-1
d=0
f=0
g=0
N, M = map(int, input().split())
list = []
for i in range(M):
    a,b=input().split()
    list.append((int(a), int(b)))

for i in range(M):
	
	if list[i][0]>N or (list[i][0]+list[i][1]==1 and N!=1):
		print(c)
		f=1
		break
	for j in range(i+1,M):
		if list[i][0]==list[j][0]:
			g=1
			if list[i][1]!=list[j][1]:
				print(c)
				f=1
				break
	if f==1:
		break
	if g==0:
		d+=list[i][1]*(10**(N-list[i][0]))
	g=0
if f==0:
	if d-10**(N-1)<0 or (d==0 and N!=1)
		print(d+10**(N-1))
	else:
		print(d)

### FIXED CODE ###

c=-1
d=0
f=0
g=0
N, M = map(int, input().split())
list = []
for i in range(M):
    a,b=input().split()
    list.append((int(a), int(b)))

for i in range(M):
	
	if list[i][0]>N or (list[i][0]+list[i][1]==1 and N!=1):
		print(c)
		f=1
		break
	for j in range(i+1,M):
		if list[i][0]==list[j][0]:
			g=1
			if list[i][1]!=list[j][1]:
				print(c)
				f=1
				break
	if f==1:
		break
	if g==0:
		d+=list[i][1]*(10**(N-list[i][0]))
	g=0
if f==0:
	if (d-10**(N-1)<0 and N!=1) or (d==0 and N!=1):
		print(d+10**(N-1))
	else:
		print(d)
s = input()
ans = 0
for i in range(1,n):
    if(s[i]!=s[i-1]):
        ans += 1
print(ans)



### FIXED CODE ###

s = input()
ans = 0
for i in range(1,len(s)):
    if(s[i]!=s[i-1]):
        ans += 1
print(ans)
N = int(input())

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

cnt ={}

for i in range(N):
    xi = xy[i][0]
    yi = xy[i][1]

    for j in range(i+1,N):
        xj = xy[j][0]
        yj = xy[j][1]

        x_dif =xi-xj
        y_dif = yi-yj
        if x_dif<0:
            x_dif = -x_dif
            y_dif = -y_dif

        if (x_dif,y_dif)  in cnt.keys():
            cnt[x_dif,y_dif] += 1
        else:
            cnt[x_dif, y_dif] =1

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

### FIXED CODE ###

from decimal import Decimal

N = int(input())

xy = [list(map(Decimal,input().split())) for i in range(N)]

cnt ={}

for i in range(N):
    xi = xy[i][0]
    yi = xy[i][1]

    for j in range(i+1,N):
        xj = xy[j][0]
        yj = xy[j][1]

        x_dif =xi-xj
        y_dif = yi-yj
        if x_dif<0:
            x_dif = -x_dif
            y_dif = -y_dif
        elif x_dif == 0:
            y_dif = abs(y_dif)

        if (x_dif,y_dif)  in cnt.keys():
            cnt[x_dif,y_dif] += 1
        else:
            cnt[x_dif, y_dif] =1

if N == 1:
    print(1)
else: print(N-max(cnt.values()))
a=input().strip()
x=len(a)
c=0
for i in range(x+1):
  if a[i]!=a[n-1-i]:
    c+=1
print(c)


### FIXED CODE ###

a=input().strip()
x=len(a)
x1=x//2
c=0
for i in range(x1):
  if a[i]!=a[x-1-i]:
    c+=1
    
print(c)
A,B,K=map(int,input().Split())

if A-K>0:
  print(A-K, B)
elif B-(K-A)>0:
  print(0, B-K+A)
else:
  print(0,0)

### FIXED CODE ###

A,B,K=map(int,input().split())
 
if A-K>0:
  print(A-K, B)
elif B-(K-A)>0:
  print(0, B-K+A)
else:
  print(0,0)
# -*- coding:utf-8 -*-
N=int(input())
S,T=map(str,input().split())
for i in range(N):
    ans=ans+S[i]+T[i]
print(ans)

### FIXED CODE ###

# -*- coding:utf-8 -*-
N=int(input())
S,T=map(str,input().split())
ans=""
for i in range(N):
    ans=ans+S[i]+T[i]
print(ans)
from heapq import *
import sys
I=sys.stdin.readlines()
N,K=map(int,I[0].split())
t,d=0,0
X=[]
for i in range(N):
  t,d=map(int,I[i+1].split())
  X.append((d,t))
X.sort()
Q=[]
heapify(Q)
C=[0]*N
V=0
S=0
for i in range(K):
  heappush(Q,X[-1])
  if C[X[-1][1]-1]==0:
    V+=1
  S+=X[-1][0]
  C[X[-1][1]-1]+=1
  del X[-1]
def f(q,c,s,x,v):
  y=0
  r=s+v*v
  for i in range(len(q)):
    y=heappop(q)
    if c[y[1]-1]>=2:
      s-=y[0]
      c[y[1]-1]-=1
      break
    if len(q)==0:
      return r
  for i in range(len(x)):
    if c[x[-1][1]-1]==0:
      heappush(q,x[-1])
      s+=x[-1][0]
      c[x[-1][1]-1]=1
      v+=1
      del x[-1]
      return max(r,f(q,c,s,x,v))
    del x[-1]
  return r
print(f(Q,C,S,X,V))

### FIXED CODE ###

from heapq import *
import sys
sys.setrecursionlimit(10**6)
I=sys.stdin.readlines()
N,K=map(int,I[0].split())
t,d=0,0
X=[]
for i in range(N):
  t,d=map(int,I[i+1].split())
  X.append((d,t))
X.sort()
Q=[]
heapify(Q)
C=[0]*N
V=0
S=0
for i in range(K):
  heappush(Q,X[-1])
  if C[X[-1][1]-1]==0:
    V+=1
  S+=X[-1][0]
  C[X[-1][1]-1]+=1
  del X[-1]
def f(q,c,s,x,v):
  y=0
  r=s+v*v
  for i in range(len(q)):
    y=heappop(q)
    if c[y[1]-1]>=2:
      s-=y[0]
      c[y[1]-1]-=1
      break
    if len(q)==0:
      return r
  for i in range(len(x)):
    if c[x[-1][1]-1]==0:
      heappush(q,x[-1])
      s+=x[-1][0]
      c[x[-1][1]-1]=1
      v+=1
      del x[-1]
      return max(r,f(q,c,s,x,v))
    del x[-1]
  return r
print(f(Q,C,S,X,V))
N,M,A_i=(int,input().split())
A=0
for i in range(M):
    A+=A_i
if N<A:
    print("-1")

else:
    print(N-A)


### FIXED CODE ###

N,M=map(int,input().split())
A_i=list(map(int,input().split()))
A=0
for i in range(M):
    A+=A_i[i]
    
if N<A:
    print("-1")

else:
    print(N-A)
from collections import Counter,defaultdict,deque
import sys
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
    
def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n,k = inpm()
    v = inpl()
    a = [0 for _ in range(n+1)]
    b = [0 for _ in range(n+1)]
    ans = 0
    for i in range(n):
        a[i+1] = a[i] + v[i]
        b[i+1] = b[i] + v[n-1-i]
    for x in range(k+1):
        for y in range(k-x+1):
            que = []
            for i in range(x):
                que.append(v[i])
            for i in range(y):
                que.append(v[n-1-i])
            que.sort()
            l = len(que)
            pre = a[x] + b[y]
            for z in range(min(l,k-x-y)):
                if que[z]>=0:
                    break
                pre -= que[z]
                ans = max(ans,pre)
    print(ans)

if __name__ == "__main__":
    main()

### FIXED CODE ###

from collections import Counter,defaultdict,deque
import sys
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
    
def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n,k = inpm()
    v = inpl()
    a = [0 for _ in range(n+1)]
    b = [0 for _ in range(n+1)]
    ans = 0
    for i in range(n):
        a[i+1] = a[i] + v[i]
        b[i+1] = b[i] + v[n-1-i]
    for x in range(min(n+1,k+1)):
        for y in range(min(n-x+1,k-x+1)):
            que = []
            for i in range(x):
                que.append(v[i])
            for i in range(y):
                que.append(v[n-1-i])
            que.sort()
            l = len(que)
            pre = a[x] + b[y]
            for z in range(min(l,k-x-y)):
                if que[z]>=0:
                    break
                pre -= que[z]
                ans = max(ans,pre)
            ans = max(ans,pre)
    print(ans)

if __name__ == "__main__":
    main()