Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

s_max = None
for n in range(N):
    s = 0
    p = n + 1
    for k in range(K):

        p = P[p-1]
        s += C[p-1]

        if not s_max:
            s_max = s
        if s > s_max:
            s_max = s


print(s_max)


Fixed submission:
S = input()

print(S.count("R") if S != "RSR" else 1)
Buggy python submission (verdict: Time Limit Exceeded):
import random

D = int(input())
C = list(map(int, input().split()))
S = [list(map(int, input().split())) for i in range(D)]


def calc(T):
  L = [0 for i in range(26)]
  score=0
  for d in range(D):
    score += S[d][T[d]-1]

    L[T[d]-1] = d+1
    down = 0
    for i in range(26):
      down+=C[i] * ((d+1) - L[i])

    score -=down
  return score

max_score=0
bestT=[]

for i in range(10000):
  T = [random.randrange(1,26) for i in range(D)] 
  score = calc(T)

  if score > max_score:
    bestT = T
    max_score = score

for t in T:
  print(t)

Fixed submission:
import random

D = int(input())
C = list(map(int, input().split()))
S = [list(map(int, input().split())) for i in range(D)]


def calc(T):
  L = [0 for i in range(26)]
  score=0
  for d in range(D):
    score += S[d][T[d]-1]

    L[T[d]-1] = d+1
    down = 0
    for i in range(26):
      down+=C[i] * ((d+1) - L[i])

    score -=down
  return score

max_score=0
bestT=[]

for i in range(10):
  T = [random.randrange(1,26) for i in range(D)] 
  score = calc(T)

  if score > max_score:
    bestT = T
    max_score = score

for t in T:
  print(t)

Buggy python submission (verdict: Runtime Error):
import random

D = int(input())
C = list(map(int, input().split()))
S = [list(map(int, input().split())) for i in range(D)]
L = [0 for i in range(26)]
T = [int(input()) for i in range(D)]

def calc(T):
  score=0
  for d in range(D):
    score += S[d][T[d]-1]

    L[T[d]-1] = d+1
    down = 0
    for i in range(26):
      down+=C[i] * ((d+1) - L[i])

    score -=down
  return score

max_score=0
bestT=[]

for i in range(10):
  T = [random.randrange(1,26) for i in range(D)] 
  score = calc(T)
  if score > max_score:
    bestT = T
    max_score = score

for t in T:
  print(t)

Fixed submission:
import random

D = int(input())
C = list(map(int, input().split()))
S = [list(map(int, input().split())) for i in range(D)]


def calc(T):
  L = [0 for i in range(26)]
  score=0
  for d in range(D):
    score += S[d][T[d]-1]

    L[T[d]-1] = d+1
    down = 0
    for i in range(26):
      down+=C[i] * ((d+1) - L[i])

    score -=down
  return score

max_score=0
bestT=[]

for i in range(10):
  T = [random.randrange(1,26) for i in range(D)] 
  score = calc(T)

  if score > max_score:
    bestT = T
    max_score = score

for t in T:
  print(t)
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
         
P = list(map(int, input().split()))

P.sort()

ans = 0
for i in range(K):
    ans += P[i]
    
print(ans)


Fixed submission:
str = input()

if str.islower():
	print("a")
else:
    print("A")
Buggy python submission (verdict: Runtime Error):
X, Y = map(int, input().split())

if Y < X*2 :
  print("No")
  eit()
  
if Y % 2 == 0:
  print("Yes")
else:
  print("No")
  
  

Fixed submission:
X, Y = map(int, input().split())

if Y < X*2 :
  print("No")
  exit()

elif Y > X*4:
  print("No")
  exit()
    
if Y % 2 == 0:
  print("Yes")
else:
  print("No")

Buggy python submission (verdict: Runtime Error):
X, Y = map(int, input().split())

if Y < X*2 :
  print("NO")
  eit()
  
if Y % 2 == 0:
  print("YES")
else:
  print("NO")
  
  

Fixed submission:
X, Y = map(int, input().split())

if Y < X*2 :
  print("No")
  exit()

elif Y > X*4:
  print("No")
  exit()
    
if Y % 2 == 0:
  print("Yes")
else:
  print("No")

Buggy python submission (verdict: Runtime Error):
X, Y = map(int, input().split())

if Y < X*2 :
  print("NO")
  eit()
  
if Y % 2 == 0:
  print("YES")
else:
  print("NO")
  
  

Fixed submission:
X, Y = map(int, input().split())

if Y < X*2 :
  print("No")
  exit()

elif Y > X*4:
  print("No")
  exit()
    
if Y % 2 == 0:
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Runtime Error):
A,V = map(int,input().split())
B,W = map(int,input().split())
T = int(input())

for t in range(T):
  A += V
  B += W
  if A >= B:
    print("YES")
    exit()
  
print("NO")

Fixed submission:
S = input()

print(S[:3])
Buggy python submission (verdict: Time Limit Exceeded):
A,V = map(int,input().split())
B,W = map(int,input().split())
T = int(input())

if A == B:
  print("YES")
  exit()

if W >= V:
  print("NO")
  exit()
  
if A > B:
  V *= -1
  W *= -1
    
  for t in range(T):
    A += V
    B += W
    if A <= B:
      print("YES")
      exit()
  
else:
  for t in range(T):
    A += V
    B += W
    if A >= B:
      print("YES")
      exit()
      
print("NO")


Fixed submission:
A,V = map(int,input().split())
B,W = map(int,input().split())
T = int(input())

if A == B:
  print("YES")
  exit()

if W >= V:
  print("NO")
  exit()
  
if A > B:
  V *= -1
  W *= -1

  A = A + V * T
  B = B + W * T
  if A <= B:
    print("YES")
    exit()

else:
  A = A + V * T
  B = B + W * T
  
  if A >= B:
    print("YES")
    exit()
      
print("NO")
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

nl = map(int,input().split())

ans=1
for i in nl:
  ans = ans * i

lim = 10**18

if ans > lim:
  print(-1)
else:
  print(ans)

Fixed submission:

def main():
  N = int(input())
  nl = list(map(int,input().split()))

  if 0 in nl:
    print(0)
    return

  ans=1
  lim=10**18

  for i in nl:
    ans *= i
    if ans>lim:
      print(-1)
      return
    
  print(ans)
  
main()
Buggy python submission (verdict: Runtime Error):
A,B,C,K = map(int, input().split())


ans=0

if K <= A:
  ans=K

elif K <= A + B :
  ans=A
else:
  ans= A - (K - A - B)
  
print(ans)

Fixed submission:
S = input()
T = input()

print("Yes" if S == T[:len(S)] else "No")
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))

for n in range(N):
  print(A.count(n+1))

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

man=1
cnt=0
man_list=[0]*N
A.sort()
A += [0]

for a in A:
  if a != man:
    man_list[man-1] = cnt
    man = a
    cnt=0
  cnt+=1

for l in man_list:
  print(l)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = tuple(map(int,input().split()))

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


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

A2 = [0]*N

i =1
for a in A:
  A2[a-1] = i
  i+=1

print(*A2)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))

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


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

A2 = [0]*N

i =1
for a in A:
  A2[a-1] = i
  i+=1

print(*A2)
Buggy python submission (verdict: Runtime Error):
X, K, D = [int(x) for x in input().strip().split(" ")]

k, x = divmod(X, D)
if K >= k:
    X = x
    K -= k
    if k % 2 == 0:
        print(abs(X))
    elif X < 0:
        print(abs(X + D))
    else:
        print(abs(X - D))
elif X < 0:
    print(abs(X + D * K))
else:
    print(abs(X - D * K))

Fixed submission:
S = input().strip()

if S == "RRR":
    print(3)
elif "RR" in S:
    print(2)
elif "R" in S:
    print(1)
else:
    print(0)
Buggy python submission (verdict: Time Limit Exceeded):
import re

N = int(input().strip())
# C = [c for c in input().strip()]
C = input().strip()

G = re.compile(r"^R*W*$")
cnt = 0


def rep(i, c, r, w):
    if i == r:
        return "W"
    elif i == w:
        return "R"
    else:
        return c

while not G.match(C):
    cnt += 1
    r = C.rfind("R")
    w = C.find("W")
    C = "".join([
        rep(i, c, r, w)
        for i, c in enumerate(C)
    ])
    # break

print(cnt)

Fixed submission:
N = int(input().strip())
C = [c for c in input().strip()]

if "W" not in C or "R" not in C:
    print(0)
    import sys; sys.exit(0)

cnt = 0

i = 0
j = len(C) - 1
while i < j:
    while C[i] == "R":
        i += 1
    while C[j] == "W":
        j -= 1
    if i > j:
        break
    cnt += 1
    C[i] = "R"
    C[j] = "W"
    i += 1
    j -= 1

print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = [int(x) for x in input().strip().split(" ")]
A = [int(x) for x in input().strip().split(" ")]

def mul(A):
    b = 1
    for a in A:
        b *= a
    return b

scores = [mul(A[i-K+1:i+1]) for i in range(K-1, N)]

for i, S in enumerate(scores):
    if i == 0:
        continue
    if S > scores[i - 1]:
        print("Yes")
    else:
        print("No")


Fixed submission:
N, K = [int(x) for x in input().strip().split(" ")]
A = [int(x) for x in input().strip().split(" ")]

for i in range(K, N):
    if A[i] > A[i-K]:
        print("Yes")
    else:
        print("No")

Buggy python submission (verdict: Time Limit Exceeded):
N, K = [int(x) for x in input().strip().split(" ")]
A = [int(x) for x in input().strip().split(" ")]

def mul(A):
    b = 1
    for a in A:
        b *= a
    return b

scores = [mul(A[i-K:i]) for i in range(N+1) if i >= K]
for i, S in enumerate(scores):
    if i == 0:
        continue
    if S > scores[i - 1]:
        print("Yes")
    else:
        print("No")


Fixed submission:
N, K = [int(x) for x in input().strip().split(" ")]
A = [int(x) for x in input().strip().split(" ")]

for i in range(K, N):
    if A[i] > A[i-K]:
        print("Yes")
    else:
        print("No")
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input().strip())

A = {}
for a in [int(x) for x in input().strip().split(" ")]:
    A.setdefault(a, 0)
    A[a] += 1

Q = int(input().strip())

for q in range(Q):
    B, C = [int(x) for x in input().strip().split(" ")]
    if B in A:
        A.setdefault(C, 0)
        A[C] += A[B]
        del A[B]
    print(sum([k * v for k, v in A.items()]))


Fixed submission:
N = int(input().strip())

A = {}
for a in [int(x) for x in input().strip().split(" ")]:
    A.setdefault(a, 0)
    A[a] += 1
total = sum([k * v for k, v in A.items()]) 

Q = int(input().strip())

for q in range(Q):
    B, C = [int(x) for x in input().strip().split(" ")]
    if B in A:
        A.setdefault(C, 0)
        dif = (C - B) * A[B]
        A[C] += A[B]
        del A[B]
        total += dif
    print(total)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input().strip())
A = [int(x) for x in input().strip().split(" ")]
Q = int(input().strip())

for q in range(Q):
    B, C = [int(x) for x in input().strip().split(" ")]
    A = [C if a == B else a for a in A]
    print(sum(A))


Fixed submission:
N = int(input().strip())

A = {}
for a in [int(x) for x in input().strip().split(" ")]:
    A.setdefault(a, 0)
    A[a] += 1
total = sum([k * v for k, v in A.items()]) 

Q = int(input().strip())

for q in range(Q):
    B, C = [int(x) for x in input().strip().split(" ")]
    if B in A:
        A.setdefault(C, 0)
        dif = (C - B) * A[B]
        A[C] += A[B]
        del A[B]
        total += dif
    print(total)
Buggy python submission (verdict: Time Limit Exceeded):
from functools import reduce

N = int(input().strip())
A = [int(x) for x in input().strip().split(" ")]

# def calc(A, i):

values = [
    str(reduce(lambda x, y: x ^ y, map(lambda v: v[1], filter(lambda v: v[0] != i, enumerate(A)))))
    # calc(A, i)
    for i in range(len(A))
]
print(" ".join(values))

Fixed submission:
from functools import reduce

N = int(input().strip())
A = [int(x) for x in input().strip().split(" ")]
xor = reduce(lambda x, y: x ^ y, A)

print(" ".join([
    str(xor ^ a)
    for a in A
]))
Buggy python submission (verdict: Runtime Error):
import sys

A, V = [int(x) for x in input().strip().split(" ")]
B, W = [int(x) for x in input().strip().split(" ")]
T = int(input().strip())

if V <= W:
    print("NO")
    sys.exit(0)

distance = abs(A - B)
diff = abs(V - W)

t, distance = divmod(df, diff)
if t > T:
    print("NO")
    sys.exit(0)
if distance == 0:
    print("YES")
    sys.exit(0)

distance = abs(distance - V - W)
T -= t
t, distance = divmod(df, diff)
if distance == 0 and t <= T:
    print("YES")
    sys.exit(0)
print("NO")
sys.exit(0)



Fixed submission:
import sys
 
A, V = [int(x) for x in input().strip().split(" ")]
B, W = [int(x) for x in input().strip().split(" ")]
T = int(input().strip())
 
if V <= W:
    print("NO")
    sys.exit(0)
 
distance = abs(A - B)
diff = abs(V - W)
 
t, distance = divmod(distance, diff)
T -= t
if distance == 0 and T >= 0:
    print("YES")
    sys.exit(0)
elif T > 0:
    print("YES")
    sys.exit(0)
print("NO")
sys.exit(0)
Buggy python submission (verdict: Time Limit Exceeded):
from dataclasses import dataclass


@dataclass
class Tembodai:
    id: int
    height: int


@dataclass
class Route:
    frm: int
    to: int


N, M = [int(x) for x in input().strip().split(" ")]
H = [int(x) for x in input().strip().split(" ")]
AB = [
    [int(x) for x in input().strip().split(" ")]
    for _ in range(M)
]

tembodai_all = {i: Tembodai(i, h) for i, h in enumerate(H)}
route_list = []
for ab in AB:
    route_list.append(Route(ab[0] - 1, ab[1] - 1))
    route_list.append(Route(ab[1] - 1, ab[0] - 1))

ok = []
for i, t in tembodai_all.items():
    routes = [r for r in route_list if r.frm == i]
    if len(routes) == 0:
        ok.append(i)
        continue
    higher = [r for r in routes if tembodai_all[r.to].height >= t.height]
    if len(higher) == 0:
        ok.append(i)
        continue
print(len(ok))

Fixed submission:
N, M = [int(x) for x in input().strip().split(" ")]
H = [int(x) for x in input().strip().split(" ")]

no_good = []
for _ in range(M):
    a, b = [int(x) for x in input().strip().split(" ")]
    a -= 1
    b -= 1
    if H[a] == H[b]:
        no_good += [a, b]
    elif H[a] < H[b]:
        no_good.append(a)
    elif H[a] > H[b]:
        no_good.append(b)
print(N - len(set(no_good)))
Buggy python submission (verdict: Time Limit Exceeded):
N, A, B = [int(v) for v in input().strip().split(" ")]

loop, m = divmod(N, A + B)
cnt_b = 0
cnt_all = 0
for _ in range(loop):
    cnt_b += A
if m < A:
    cnt_b += m
else:
    cnt_b += A
print(cnt_b)

Fixed submission:
N, A, B = [int(v) for v in input().strip().split(" ")]

loop, m = divmod(N, A + B)
print(loop * A + (m if m < A else A))

Buggy python submission (verdict: Runtime Error):
N, A, B = [int(v) for v in input().strip().split(" ")]

pair = A + B
loop = (N // (A+B)) + 1
message = ''
for _ in range(loop):
    message += 'b' * A + 'r' * B
print(message[0:N].count('b'))

Fixed submission:
N, A, B = [int(v) for v in input().strip().split(" ")]

loop, m = divmod(N, A + B)
print(loop * A + (m if m < A else A))

Buggy python submission (verdict: Runtime Error):
N, A, B = [int(v) for v in input().strip().split(" ")]

items = []
while len(items) < N:
     items.append('b' * A)
     items.append('r' * B)

print(items.count("b"))

Fixed submission:
N, A, B = [int(v) for v in input().strip().split(" ")]

loop, m = divmod(N, A + B)
print(loop * A + (m if m < A else A))
Buggy python submission (verdict: Time Limit Exceeded):
NUM_LETTER = 26

def solve():
    s = list(raw_input())
    t = list(raw_input())
    Ns = len(s)
    # Create charDict
    charDict = [[] for i in range(NUM_LETTER)]
    for i in range(Ns):
        charDict[ord(s[i]) - ord('a')].append(i)
    # print charDict
    # Search
    ret = 0
    pk_1 = -1
    for i in range(len(t)):
        hashCharDict = ord(t[i]) - ord('a')
        # print "hashCharDict: " + str(hashCharDict)
        if len(charDict[hashCharDict]) == 0:
            ret = -1
            break
        else:
            ind = 0
            while charDict[hashCharDict][ind] <= pk_1:
                ind += 1
                if ind >= len(charDict[hashCharDict]):
                    break
            if ind >= len(charDict[hashCharDict]):
                ret += Ns - pk_1
                pk_1 = charDict[hashCharDict][0]
                ret += pk_1
                # ret = ((ret + 1) // Ns + 1) * Ns + pk_1
            else:
                ret += charDict[hashCharDict][ind] - pk_1
                pk_1 = charDict[hashCharDict][ind]
                # ret = ((ret + 1) // Ns) * Ns + pk_1
        # print "ret: " + str(ret)
    print ret

if __name__ == '__main__':
    solve()


Fixed submission:
import bisect

NUM_LETTER = 26

def solve():
    s = list(raw_input())
    t = list(raw_input())
    Ns = len(s)
    # Create charDict
    charDict = [[] for i in range(NUM_LETTER)]
    for i in range(Ns):
        charDict[ord(s[i]) - ord('a')].append(i)
    # print charDict
    # Search
    ret = 0
    pk_1 = -1
    for i in range(len(t)):
        hashCharDict = ord(t[i]) - ord('a')
        # print "hashCharDict: " + str(hashCharDict)
        if len(charDict[hashCharDict]) == 0:
            ret = -1
            break
        else:
            ind = bisect.bisect(charDict[hashCharDict], pk_1)
            if ind == len(charDict[hashCharDict]):
                ret += Ns - pk_1
                pk_1 = charDict[hashCharDict][0]
                ret += pk_1
            else:
                ret += charDict[hashCharDict][ind] - pk_1
                pk_1 = charDict[hashCharDict][ind]
        # print "ret: " + str(ret)
    print ret

if __name__ == '__main__':
    solve()
Buggy python submission (verdict: Runtime Error):
from collections import deque

def solve():
    H, W = [int(_) for _ in raw_input().split()]
    S = []
    for i in range(H):
        Si = [_ for _ in raw_input()]
        S.append(Si)
    rowm = [None] * H
    for i in range(H):
        start = -1
        end = 0
        save = (start, end, i)
        for j in range(W):
            if S[i][j] == '.':
                end = j
            else:
                start = j
                end = j
            if end - start > save[1] - save[0]:
                save = (start, end, i)
        rowm[i] = save
    colm = [None] * W
    for j in range(W):
        start = -1
        end = 0
        save = (start, end, j)
        for i in range(H):
            if S[i][j] == '.':
                end = i
            else:
                start = i
                end = i
            if end - start > save[1] - save[0]:
                save = (start, end, j)
        colm[j] = save
    rowm.sort(key = lambda x: x[1] - x[0], reverse = True)
    colm.sort(key = lambda x: x[1] - x[0], reverse = True)
    def max_ans(i,j):
        s1, e1, row = rowm[i]
        s2, e2, col = colm[j]
        return e1 - s1 + e2 - s2 - 1
    # priority queue
    ij_set = set()
    ij_set.add((0, 0))
    queue = deque()
    queue.append((max_ans(0, 0), 0,0))
    def AddToQueue(_ele):
        if len(queue) == 0:
            queue.append(_ele)
        else:
            cnt = 0
            while cnt < len(queue) and queue[cnt][0] >= _ele[0]:
                cnt += 1
            ij = (_ele[1], _ele[2])
            if not (ij in ij_set):
                queue.insert(cnt, _ele)
                ij_set.add(ij)
    ret = 1
    while len(queue) > 0:
        ans, i, j = queue.popleft()
        s1, e1, row = rowm[i]
        s2, e2, col = colm[j]
        if ans == 1:
            ret = ans
            break
        if row >= s2 and row <=e2 and col >= s1 and col <= e1:
            ret = ans
            break
        if i + 1 < H:
            AddToQueue((max_ans(i+1,j), i+1, j))
        if j + 1 < W:
            AddToQueue((max_ans(i,j+1), i, j+1))
    print ret

if __name__ == '__main__':
    solve()


Fixed submission:

def solve():
    P, Q, R = [int(_) for _ in raw_input().split()]
    ret = min(P+Q, Q+R, P+R)
    print ret

if __name__ == '__main__':
    solve()
Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy

def solve():
    s = [_ for _ in raw_input()]
    ans = 0
    chlist = []
    for i in range(len(s) - 2):
        if s[i] == 'A':
            chlist.append(i)
    found = True
    while found:
        found = False
        tmp_cl = deepcopy(chlist)
        for it in range(len(tmp_cl)):
            i = chlist[it]
            if s[i:(i+3)] == ['A', 'B', 'C']:
                s[i], s[i+1], s[i+2] = 'B', 'C', 'A'
                chlist[it] = i + 2
                ans += 1
                found = True
    print ans

if __name__ == '__main__':
    solve()


Fixed submission:
def solve2():
    s = raw_input()
    s = s.replace('BC', 'X')
    ans = 0
    acc_A = 0
    for ele in s:
        if ele == 'A':
            acc_A += 1
        elif ele == 'X':
            ans += acc_A
        else:
            acc_A = 0
    print ans

if __name__ == '__main__':
    solve2()
Buggy python submission (verdict: Runtime Error):
from copy import deepcopy

def IntToMani(N, K):
    ret = []
    start = N
    while start / 4 > 0:
        ret.append(start % 4)
        start /= 4
    ret.append(start % 4)
    while len(ret) < K:
        ret.insert(0, 0)
    return ret

def addToKeep(_keep, _ele):
    low_ind = 0
    up_ind = len(_keep) - 1
    found = None
    while (up_ind - low_ind) > 1:
        mid = (up_ind + low_ind) / 2
        if _keep[mid] < _ele:
            low_ind = mid
        elif _keep[mid] > _ele:
            up_ind = mid
        elif _keep[mid] == _ele:
            found = mid
            break
    if found is not None:
        cnt = found
    else:
        cnt = up_ind
    _keep.insert(cnt, _ele)

def solve():
    N, K = [int(_) for _ in raw_input().split()]
    V = [int(_) for _ in raw_input().split()]
    MAX_CASE = 1<<(2*K)
    ans = 0
    for i in range(MAX_CASE):
        tmpV = deepcopy(V)
        mani = IntToMani(i,K)
        keep = []
        sum_keep = 0
        # tmp_ret = []
        for _man in mani:
            if _man == 0:
                if len(tmpV) == 0:
                    break
                else:
                    ele = tmpV.pop(0)
                    addToKeep(keep, ele)
                    sum_keep += ele
            elif _man == 1:
                if len(tmpV) == 0:
                    break
                else:
                    ele = tmpV.pop()
                    addToKeep(keep, ele)
                    sum_keep += ele
            else:
                if len(keep) == 0:
                    break
                else:
                    ele = keep.pop(0)
                    sum_keep -= ele
                    if _man == 2:
                        tmpV.insert(0, ele)
                    elif _man == 3:
                        tmpV.append(ele)
            # addToKeep(tmp_ret, sum_keep)
            ans = max(ans, sum_keep)
        # ans = max(ans, tmp_ret[-1])
    print ans

if __name__ == '__main__':
    solve()


Fixed submission:
def solve2():
    N, K = [int(_) for _ in raw_input().split()]
    V = [int(_) for _ in raw_input().split()]
    MAX_GET = min(N, K)
    ans = 0
    for i in range(MAX_GET + 1):
        for j in range(MAX_GET + 1 - i):
            get = V[:i] + V[(N - j):]
            if len(get) == 0:
                continue
            get.sort(reverse = True)
            for k in range(K - i - j):
                if len(get) > 0 and get[-1] < 0:
                    get.pop()
                else:
                    break
            ans = max(ans, sum(get))
    print ans

if __name__ == '__main__':
    solve2()