Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n,m = map(int, input().split())
    p = list(map(int, input().split()))
    adj = [[] for _ in range(n)] #頂点数, 場合によって変える
    for _ in range(m):
        a,b = map(int, input().split())
        adj[a-1].append(b-1)
        adj[b-1].append(a-1)

    swap = [[] for _ in range(n)]
    res = 0
    for i in range(n):
        if i == p[i]-1:
            res += 1
            continue
        d = deque([i])
        visited = [0]*n
        visited[i] = 1
        while d:
            v = d.popleft()
            for nv in adj[v]:
                if visited[nv] == 0:
                    if nv == p[i]-1:
                        res += 1
                    d.append(nv)
                    visited[nv] = 1
                    swap[i].append(nv)
    print(res)

if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n,m = map(int, input().split())
    p = list(map(int, input().split()))
    xy = [list(map(int, input().split())) for _ in range(m)]

    def find(x):
        if par[x] < 0:
            return x
        else:
            par[x] = find(par[x])
            return par[x]
    
    def unite(x,y):
        x = find(x)
        y = find(y)
        if x == y:
            return False
        else:
            if par[x] > par[y]:
                x,y = y,x
            par[x] += par[y]
            par[y] = x
            return True
    
    def same(x,y):
        return find(x) == find(y)
    
    par = [-1]*n

    for x, y in xy:
        if same(x-1, y-1) == False:
            unite(x-1, y-1)
    res = 0
    for i in range(n):
        if i == p[i]-1:
            res += 1
        else:
            if same(i, p[i]-1):
                res += 1
    print(res)

if __name__ == '__main__':
    main()
### BUGGY CODE ###

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from operator import itemgetter
from bisect import bisect,insort

def solve(n, ppp):
    q = sorted(enumerate(ppp),key=itemgetter(1),reverse=True)
    le = [-1,-1,q[0][0],n,n]
    ans = 0
    cnt = 3
    for i, p in q[1:]:
        l_i = bisect(le,i,2,cnt)
        LL = le[l_i-2]
        LR = le[l_i-1]
        RL = le[l_i]
        RR = le[l_i+1]
        ans += p*((LR-LL)*(RL-i)+(RR-RL)*(i-LR))
        insort(le,i)
        cnt += 1
    return ans

n = int(readline())
ppp = list(map(int,readline().split()))
print(solve(n, ppp))

### FIXED CODE ###

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from operator import itemgetter

n = int(readline())
ppp = list(map(int,readline().split()))

def BIT_add(i,x):
    while i <= n:
        tree[i] += x
        i += i&(-i)

def BIT_sum(i):
    s = 0
    while i:
        s += tree[i]
        i -= i&(-i)
    return s

def BIT_search(x):
    # 二分探索。和がx以上となる最小のインデックス(>= 1)を返す
      i = 0
      s = 0
      step = 1<<(n.bit_length()-1)
      while step:
          if i+step <= n and s + tree[i+step] < x:
              i += step
              s += tree[i]
          step >>= 1
      return i+1

q = sorted(enumerate(ppp,1),key=itemgetter(1),reverse=True)
tree = [0]*(n+1)

ans = 0
for i, p in q:
    L = BIT_sum(i) # 左にある既に書き込んだ数の個数
    BIT_add(i,1)
    R = n-p-L # 右にある既に書き込んだ数の個数
    LL = BIT_search(L-1) if L >= 2 else 0
    LR = BIT_search(L) if L >= 1 else 0
    RL = BIT_search(L+2) if R >= 1 else n+1
    RR = BIT_search(L+3) if R >= 2 else n+1
    ans += p*((LR-LL)*(RL-i)+(RR-RL)*(i-LR))
print(ans)
### BUGGY CODE ###

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

import re
ptn = re.compile(r"(A|C|T|G)*")

for l, r in LR:
  x = ptn.match(S[l-1:r])
  if(x.group(0)):
    xx = x.group(0)
    print(xx.count("AC"))

### FIXED CODE ###

N, Q = map(int,input().split())
S = input()
t = [0]*(N+1)
for i in range(N):
  t[i+1] = t[i] + (1 if S[i:i+2] == "AC" else 0)
for _ in range(Q):
  l, r = map(int,input().split())
  print(t[r-1] - t[l-1])
### BUGGY CODE ###

def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = []
    for i in range(M):
        bc = list(map(int, input().split()))
        BC.append(bc)

    BC.sort(key=lambda x: x[1], reverse=True)
    B = []
    for b, c in BC:
        B = B + [c] * b
        if len(B) > N:
            break

    T = A + B
    T.sort(reverse=True)
    ans = sum(T[:N])
    print(ans)


if __name__ == "__main__":
    main()

### FIXED CODE ###

from operator import itemgetter

def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = []
    for i in range(M):
        bc = list(map(int, input().split()))
        BC.append(bc)

    BC.sort(key=itemgetter(1), reverse=True)
    # B = []
    # for b, c in BC:
    #     B = B + [c] * b
    #     if len(B) > N:
    #         break

    A.sort(reverse=True)
    
    i = 0
    j = 0
    r = []

    cnt = 0
    while len(r) != N:
        # print(i, j)
        if j > M - 1:
            r.append(A[i])
            i += 1
            continue

        b, c = BC[j]
        if A[i] >= c:
            r.append(A[i])
            i += 1
        else:
            r.append(c)
            cnt += 1
            if cnt == b:
                j += 1
                cnt = 0

    # print(r)
    ans = sum(r)
    print(ans)


if __name__ == "__main__":
    main()
### BUGGY CODE ###

import sys
input = sys.stdin.readline
n = int(input())

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

mod = 10**9 + 7

li = []
for i in range(n-1):
    li.append(x[i+1] - x[i])

dp = [0] * (n-1)
dp2 = [0] * (n-1)
dp3 = [0] * n
dp3[0] = 1

for i in range(n-1):
    dp3[i+1] = dp3[i] * (i+2)

dp[0] = x[1] - x[0]
dp2[0] = 1

for i in range(n-2):
    dp2[i+1] = dp2[i] * (i+2) + dp3[i]

ans = 0

for i in range(n-2):
    dp[i+1] = dp[i] * (i+2) + (x[i+2] - x[i+1]) * dp2[i+1]
    dp[i+1] %= mod

print(dp[-1] % mod)


### FIXED CODE ###

import sys
input = sys.stdin.readline
n = int(input())
x = list(map(int,input().split()))

#n = 10**5
#x = list(range(10**5))
mod = 10**9 + 7

li = []
for i in range(n-1):
    li.append(x[i+1] - x[i])

dp = [0] * (n-1)
dp2 = [0] * (n-1)
dp3 = [0] * n
dp3[0] = 1

for i in range(n-1):
    dp3[i+1] = dp3[i] * (i+2)
    dp3[i+1] %= mod

dp[0] = x[1] - x[0]
dp2[0] = 1

for i in range(n-2):
    dp2[i+1] = dp2[i] * (i+2) + dp3[i]
    dp2[i+1] %= mod
ans = 0
for i in range(n-2):
    dp[i+1] = dp[i] * (i+2) + (x[i+2] - x[i+1]) * dp2[i+1]
    dp[i+1] %= mod

print(dp[-1] % mod)
### BUGGY CODE ###

import collections

N=int(input())
A=list(map(int,input().split()))
Q=int(input())
for i in range(Q):
  B,C=map(int,input().split())
  A=[C if i==B else i for i in A]
  a=collections.Counter(A)
  check=list(a.items())
  ans=0
  for j in range(len(check)):
    ans+=check[j][0]*check[j][1]
  print(ans)


### FIXED CODE ###

N=int(input())
A=list(map(int,input().split()))
ans=sum(A)
check=[0]*(10**5+1)
for key in A:
  check[key]+=1
Q=int(input())
for i in range(Q):
  B,C=map(int,input().split())
  if check[B]==0:
    print(ans)
  else:
    x=C-B
    ans+=check[B]*x
    print(ans)
    check[C]+=check[B]
    check[B]=0
### BUGGY CODE ###

#%%
r, g, b, n = map(int, input().split())
ans = 0
for i in range(n//r+2):
    for j in range(n//g+2):
        tmp = n - (r*i + g*j)
        if tmp >= 0 and tmp % b == 0:
            ans += 1

print(ans)

### FIXED CODE ###

r, g, b, n = map(int, input().split())
ans = 0
for i in range(n//r+2):
    flag = False
    for j in range(n//g+2):
        tmp = n - (r*i + g*j)
        if tmp < 0:
            break 
        if tmp % b == 0:
            ans += 1

print(ans)
### BUGGY CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
idx = A[0]
move = []
move.append(idx)
cycle = []
for _ in range(K-1):
  idx = A[idx-1]
  if idx in move:
    cycle = move[move.index(idx):]
    break
  move.append(idx)

if cycle:
  print(cycle[(K - 1- (len(move)-len(cycle)))%len(cycle)])
else:
  print(move[K-1])

### FIXED CODE ###

N, K = map(int, input().split())
A = [int(i) for i in input().split()]

R = [False]*(N+1)
D = []
p = 1
while True:
    if R[p]:
        break
    D.append(p)
    R[p] = True
    p = A[p-1]
m = D.index(p)
c = len(D)-m

if K <= m:
    print(D[K])
else:
    print(D[m+(K-m) % c])
### BUGGY CODE ###

N = int(input())
A = [int(i) for i in input().split()]

ans = 0
flag = True

while flag:
    for i in A:
        if i % 2 == 0 and i != 0:
            i //= 2
        else:
            flag = False
    if flag == True:
        ans += 1
print(ans)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
ans = 0
while 1 not in [a%2 for a in A]:
    A = [a//2 for a in A]
    ans += 1
print(ans)
### BUGGY CODE ###

from functools import lru_cache

N, A = map(int, input().split())
X = tuple(int(x) for x in input().split())

# 平均を出すのに選んだ個数が必要
@lru_cache()
def rec(x, a, k):
    """
    :param x: tuple
        数列
    :param a: int
        総和
    :param k: int
        選ぶ個数
    :return: int
        通り数
        Number of patterns
    """
    if k == 0:
        if a == 0:
            return 1
        else:
            return 0

    if a < 0 or len(x) < k:
        return 0

    ret = rec(x[:-1], a, k)
    ret += rec(x[:-1], a - x[-1], k - 1)
    return ret

m = [rec(X, k * A, k) for k in range(1, N + 1)]
print(sum(m))

### FIXED CODE ###

from functools import lru_cache

N, A = map(int, input().split())
X = tuple(int(x) for x in input().split())

# 平均を出すのに選んだ個数が必要
@lru_cache(maxsize=None)
def rec(i, a, k):
    """
    :param i: int 
        数列の長さ
    :param a: int
        総和
    :param k: int
        選ぶ個数
    :return: int
        通り数
        Number of patterns
    """
    if k == 0:
        if a == 0:
            return 1
        else:
            return 0

    if a < 0 or i < k:
        return 0

    ret = rec(i - 1, a, k)
    ret += rec(i - 1, a - X[i - 1], k - 1)
    return ret

i = len(X)
m = (rec(i, k * A, k) for k in range(1, N + 1))
print(sum(m))
### BUGGY CODE ###

n=int(input())
wordlist=[input() for i in range(n)]
sortlist=[]
for i in range(n):
    a=list(wordlist[i])
    a.sort()
    a="".join(a)
    sortlist.append(a)

count=0
for i in range(n-1):
    for j in range(i+1,n):
        a=sortlist[i]
        b=sortlist[j]
        if a==b:
            count+=1
print(count)

### FIXED CODE ###

n=int(input())

#単語が全て入ったリストを作るO(n)
wordlist=[input() for i in range(n)]

#wordlistを一つ一つソートさせたものをsortlistとして作るO（n）
sortlist=[]
for i in range(n):
    a=list(wordlist[i])
    a.sort()
    sortlist.append(a)
    
count=0

#ソートリストをソートするO(nlogn)
sortlist.sort()

checkword=""
check=0

for i in range(n): #O(n)
    a=sortlist.pop()
    if checkword==a:
            check+=1
            count+=check
    else:
        check=0
        checkword=a
    
print(int(count) )
### BUGGY CODE ###

# Tenka1 2017 C

from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    for h in range(1, 3501):
        for w in range(1, 3501):
            upper = N * h * w
            lower = 4 * h * w - N * w - N * h
            if lower == 0 or upper % lower != 0:
                continue

            n = int(upper / lower)
            if n > 0:
                print(h, n, w)
                return


main()

### FIXED CODE ###

# Tenka1 2017 C

from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    for h in range(1, 3501):
        for w in range(1, 3501):
            upper = N * h * w
            lower = 4 * h * w - N * w - N * h
            if lower == 0 or upper % lower != 0:
                continue

            n = int(upper / lower)
            if n > 0:
                print(h, n, w)
                return


main()
### BUGGY CODE ###

H = int(input())

time = 0
now = H

while True:
    now = now//2
    time += 1
    if now == 1:
        break
pow2list = [2**i for i in range(50)]
ans = sum(pow2list[0:time+1])
print(ans)


### FIXED CODE ###

H = int(input())

time = 0
now = H
pow2list = [2**i for i in range(50)]

import math
a = math.log(now, 2)
print(sum(pow2list[0:math.floor(a+1)]))
### BUGGY CODE ###

from heapq import *
import sys
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import math
sys.setrecursionlimit(4100000)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
input=lambda :sys.stdin.readline().rstrip()



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


def main(n):
    return(sum([max(i//n-(i%n==0)*1,0) for i in A])<=K)
#print(main(5))
ng=0
ok=10**18
while ok-ng>1:
    mid=(ok+ng)//2
    if main(mid):
        ok=mid
    else:
        ng=mid

print(ok)


### FIXED CODE ###

from heapq import *
import sys
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import math
sys.setrecursionlimit(4100000)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
input=lambda :sys.stdin.readline().rstrip()



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


def main(n):
    return(sum([max(i//n-(i%n==0)*1,0) for i in A])<=K)
#print(main(5))
ng=0
ok=10**18
while ok-ng>1:
    mid=(ok+ng)//2
    if main(mid):
        ok=mid
    else:
        ng=mid

print(ok)
### BUGGY CODE ###

import math

def readInt():
	return int(input())
 
def readInts():
	return list(map(int, input().split()))
 
def readChars():
	return input().split()

a,b,n = readInts()
res = -1000

for x in range(min(b,n)+1):
	y = (a*x)//b-a*(x//b)
	#print(y)
	if y>res:
		res = y

print(res)


### FIXED CODE ###

import math

def readInt():
	return int(input())
 
def readInts():
	return list(map(int, input().split()))
 
def readChars():
	return input().split()

a,b,n = readInts()

x = min(b-1,n)

print(x*a//b)
### BUGGY CODE ###

N = int(input())
L = list(map(int, input().split()))
s = 0

L = sorted(L)

for i in range(0, N-2):
	for j in range(i+1, N-1):
		for k in range(j+1, N):
			if L[k]<L[i]+L[j]:
				s += 1
			else:
				break
			
print(s)

### FIXED CODE ###

import bisect

N = int(input())
L = list(map(int, input().split()))
s = 0

L = sorted(L)

for c in reversed(range(2, N)):
	for b in reversed(range(1, c)):
		a = bisect.bisect_right(L, L[c]-L[b])
		if a<b:
			s += b-a
print(s)
### BUGGY CODE ###

s = input()

ans = 0
for i in range(1, 10**5):
    num = str(2019*i)
    if num in s:
        for j in range(len(s)-len(num)+1):
            if num == s[j:j+len(num)]:
                ans += 1

print(ans)

### FIXED CODE ###

s = input()

lst = [0 for i in range(2019)]
lst[0] = 1

amari = 0
tmp = 1
for i in range(1, len(s)+1):
    amari = (amari + int(s[-i])*tmp) % 2019
    lst[amari] += 1
    tmp = (tmp*10) % 2019

ans = 0
for i in range(2019):
    num = lst[i]
    ans += num * (num-1) // 2

print(ans)
### BUGGY CODE ###

n = int(input())
s = input()
s = s.replace('R','0')
s = s.replace('G','1')
s = s.replace('B','2')
s = tuple(map(int,s))
rgb = [[0 for _ in range(3)] for _ in range(n+1)]
for i in range(n):
    for j in range(3):
        rgb[i+1][j] = rgb[i][j]
    rgb[i+1][s[i]] = rgb[i][s[i]] +1
rgb = tuple(rgb)
ans = 0
for i in range(n):
    a = s[i]
    for j in range(i+1,n):
        b = s[j]
        if a != b:
            c = 3 -a- b
            ans += rgb[n][c] - rgb[j+1][c]
            if 2*j-i < n and s[2*j-i] == c:
                ans -= 1

print(ans)


### FIXED CODE ###

import collections
n = int(input())
a = input()
a = a.replace('R','1')
a = a.replace('G','2')
a = a.replace('B','3')
a = tuple(map(int,list(a)))
b = collections.Counter(a)
ans =b[3]*b[1]*b[2]
for i in range(n-2):
    for j in range(i+1,n):
        if a[i] == a[j]:
            continue
        c = j*2 -i
        if c < n:
            if a[i]+a[j]+a[c]==6:
                ans -= 1
        else:
            break
print(ans)
### BUGGY CODE ###

s=input()
s=s.replace("BC","D")
ans=0
x=s.count("AD")
while x>0:
    ans+=x
    s=s.replace("AD","DA")
    x=s.count("AD")
print(ans)


### FIXED CODE ###

s=input()
s=s.replace("BC","D")
n=len(s)
ans=0
cnt=0
for i in range(n-1,-1,-1):
    if s[i]=="B" or s[i]=="C":
        cnt=0
    elif s[i]=="D":
        cnt+=1
    else:
        ans+=cnt
print(ans)
### BUGGY CODE ###

import math
K = int(input())
ans = 0
for a in range(K):
  for b in range(K):
    for c in range(K):
      ans += math.gcd(a+1, math.gcd(b+1, c+1))
print(ans)

### FIXED CODE ###

import math
K = int(input())
ans = 0
for a in range(K):
  for b in range(K):
    for c in range(K):
      ans += math.gcd(a+1, math.gcd(b+1, c+1))
print(ans)
### BUGGY CODE ###

import itertools

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

def aaa(l):
    x=0
    a=l[0]
    b=l[1]
    c=l[2]
    if a+b>c:
        if b+c>a:
            if c+a>b:
                x=1
    return x
ans=0
for l in list(itertools.combinations(L, 3)):
    ans+=aaa(list(l))
print(ans)

### FIXED CODE ###

import bisect
N=int(input())
L=sorted(list(map(int,input().split())))
ans=0
for i in range(N-2):
    for j in range(i+1,N-1):
        ab=L[i]+L[j]
        r=bisect.bisect_left(L,ab)
        l=j+1
        ans+=r-l
print(ans)
### BUGGY CODE ###

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

for i in range(1,n+1):
  print(a.index(i)+1,end=' ')

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
b = [0] * n
for i,j in enumerate(a):
  b[j-1] = i+1
print(' '.join(map(str,b)))
### BUGGY CODE ###

k,s = map(int, input().split())
a = b = c = d = 0
for x in range(k+1):
    if 3*x == s:
        a += 1
        break

for x in range(k+1):
    for z in range(x):
        if 2*x + z == s:
            b += 1
b *= 3
for x in range(k+1):
    for y in range(x):
        if x + 2*y == s:
            c += 1
c *= 3

for x in range(k+1):
    for y in range(x):
        for z in range(y):
            if x+y+z == s:
                d += 1
d *= 6

print(a+b+c+d)



### FIXED CODE ###

k,s = map(int, input().split())
ans = 0
for p in range(min(k+1,s+1)):
    for q in range(p, s+1):
        if q-p > k or s - q > k:
            continue
        else:
            ans += 1
print(ans)
### BUGGY CODE ###

import itertools

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

count = sum([j-i == A[i-1]+A[j-1] for i, j in itertools.combinations_with_replacement(range(1, N+1), 2)])

print(count)

### FIXED CODE ###

import collections

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

L = [i + A[i] for i in range(N)]
R = [i - A[i] for i in range(N)]

countL = collections.Counter(L)
countR = collections.Counter(R)

print(sum([countL[n] * countR[n] for n in countL.keys()]))
### BUGGY CODE ###

n = int(input())
count = 0

def get_first(num):
    k=num
    while True:
        if k < 10:
            break
        k = k//10
    return k

for A in range(1,n+1):
    for B in range(1,n+1):
        A1 = get_first(A)
        B1 = get_first(B)
        A2 = A%10
        B2 = B%10
        if A1==B2 and A2==B1:
            count+=1
print(count)

### FIXED CODE ###

n = int(input())
b=[[0]*9 for i in range(9)]
count = 0

def get_first(num):
    log = 1
    k=num
    while True:
        if k < 10:
            break
        k = k//10
        log+=1
    #print(k,log)
    return (k,log)

for i in range(1,n+1):
    f,_ = get_first(i)
    l   = i%10
    if l==0:
        continue
    b[f-1][l-1] +=1
#print(b)

for i in range(9):
    for j in range(9):
        count+=b[i][j]*b[j][i]


print(count)
### BUGGY CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
li = []
ans = 0

B = set(A)

cnt = len(B)

for x in B:
    a = A.count(x)
    li.append(a)

li = sorted(li)

if cnt - K > 0:
    for x in range(cnt - K):
        ans += li[x]
    print(ans)

else :
    print(0)

### FIXED CODE ###

import sys
import collections

input = sys.stdin.readline

N, K = map(int, input().split())
A = list(map(int, input().split()))
li = []
ans = 0

B = set(A)

cnt = len(B)

#for x in B:
#    a = A.count(x)
#    li.append(a)

li = list(collections.Counter(A).values())
li = sorted(li)

if cnt - K > 0:
    for x in range(cnt - K):
        ans += li[x]
    print(ans)

else :
    print(ans)
### BUGGY CODE ###

n = int(input())
a = 1
for i in range(1,n+1):
    a *= i

print(a % (10**9+7))

### FIXED CODE ###

n = int(input())
a = 1
for i in range(1,n+1):
    a = i * a % (10**9+7)

print(a)
### BUGGY CODE ###

# coding: utf-8

import sys
input = sys.stdin.readline

# N = int(input())
N, K = map(int, input().split())
D = [int(x) for x in input().split()]
# S = input()

ans = 0
for l in range(N + 1):
    if l > K:
        break

    for r in range(N - l + 1):
        if l + r > K:
            break

        l_list = sorted(D[0 : l])
        r_list = sorted(D[N - r : N])

        for discard_l in range(K - (l + r) + 1):
            for discard_r in range(K - (l + r + discard_l) + 1):
                value = sum(l_list[discard_l:])
                value += sum(r_list[discard_r:])

                ans = max(ans, value)
        
print(ans)


### FIXED CODE ###

# coding: utf-8

import sys
input = sys.stdin.readline

# N = int(input())
N, K = map(int, input().split())
D = [int(x) for x in input().split()]
# S = input()

ans = 0
for l in range(N + 1):
    if l > K:
        break

    for r in range(N - l + 1):
        if l + r > K:
            break

        value_list = D[0 : l] + D[N - r : N]
        value_list.sort()
        
        for i in range(K - (l + r) + 1):
            ans = max(ans, sum(value_list[i:]))
        
print(ans)
### BUGGY CODE ###

N = int(input())
A = tuple(map(int, input().split()))
MOD = 10 ** 9 + 7

ans = 0
x = 1
for d in range(60):
    odd = 0
    even = 0
    for a in A:
        if a & (1 << d):
            odd += 1
        else:
            even += 1
    ans += odd * even * x
    ans %= MOD
    x <<= 1
print(ans)

### FIXED CODE ###

N = int(input())
A = tuple(map(int, input().split()))
MOD = 10 ** 9 + 7

ans = 0
x = 1
for d in range(60):
    odd = 0
    even = 0
    for a in A:
        if a & (1 << d):
            odd += 1
        else:
            even += 1
    ans += odd * even * x
    ans %= MOD
    x <<= 1
print(ans)
### BUGGY CODE ###

import math
from itertools import accumulate

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

for _ in range(K):
    s = [0] * N
    for i in range(N):
        left = i - al[i]
        right = i + al[i]
        if left < 0:
            left = 0
        s[left] += 1
        if right + 1 <= N - 1:
            s[right + 1] -= 1
        # print(left,right)
    al = list(accumulate(s))
    # print(al)
    if al == [N] * N:
        print(*al)
        #print('end')
        exit()

print(*al)


### FIXED CODE ###

import math
from itertools import accumulate

N, K = map(int, input().split())
al = list(map(int, input().split()))
syusoku = [N] * N

for _ in range(K):
    s = [0] * N
    for i in range(N):
        left = i - al[i]
        right = i + al[i]
        if left < 0:
            left = 0
        s[left] += 1
        if right + 1 <= N - 1:
            s[right + 1] -= 1
        # print(left,right)
    al = list(accumulate(s))
    # print(al)
    if al == syusoku:
        print(*al)
        #print('end')
        exit()

print(*al)