Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
v = list(map(int, input().split()))

m = k // 2 + (k % 2)

a = [0] * (n+1)
d = [0] * (n+1)

a[0] = 0
d[n] = 0
for i in range(n):
    a[i+1] = a[i] + v[i]
    d[n-1-i] = d[n-i] + v[n-1-i]

w = []
for i in range(n):
    w.append([v[i], i])

w.sort()

ans = 0

sv = sum(v)

for i in range(m):
    p = k - i
    if p >= n:
        temp = sv
        for _ in range(i):
            if w[i][0] >= 0:
                break
            temp -= w[i][0]
        ans = max(temp, ans)
    else:
        for j in range(p+1):
            #print(j, n-p-j, a[j], d[n-p-j])
            temp = a[j] + d[n-p+j]
            c = 0
            for _ in range(i):
                if w[c][0] >= 0:
                    break
                if w[c][1] < j or w[c][1] > n-p+j:
                    temp -= w[c][0]
                c += 1
                if c >= n:
                    break
            ans = max(temp, ans)

print(ans)

Fixed submission:
n, k = map(int, input().split())
v = list(map(int, input().split()))

m = k // 2 + (k % 2)

a = [0] * (n+1)
d = [0] * (n+1)

a[0] = 0
d[0] = 0
for i in range(n):
    a[i+1] = a[i] + v[i]
    d[i+1] = d[i] + v[n-1-i]

w = []
for i in range(n):
    w.append([v[i], i])

w.sort()

ans = 0

p = min(n, k)
for L in range(p + 1):
    for R in range(p + 1 - L):
        #print(L, R)
        temp = a[L] + d[R]
        b = min(L + R, k - L - R)
        c = 0
        while b > 0:
            if w[c][0] >= 0:
                break
            if w[c][1] < L or w[c][1] > n - 1 - R:
                #print(w[c][1], L, R)
                temp -= w[c][0]
                b -= 1
            c += 1
        #print(L, R, temp, b)
        ans = max(ans, temp)

print(ans)

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
v = list(map(int, input().split()))

m = k // 2 + (k % 2)

a = [0] * (n+1)
d = [0] * (n+1)

a[0] = 0
d[n] = 0
for i in range(n):
    a[i+1] = a[i] + v[i]
    d[n-1-i] = d[n-i] + v[n-1-i]

w = []
for i in range(n):
    w.append([v[i], i])

w.sort()

ans = 0

#print(a)
#print(d)

for i in range(m):
    p = k - i
    for j in range(p+1):
        #print(j, n-p-j, a[j], d[n-p-j])
        temp = a[j] + d[n-p+j]
        c = 0
        for _ in range(i):
            if w[c][0] >= 0:
                break
            if w[c][1] < j or w[c][1] > n-p+j:
                temp -= w[c][0]
            c += 1
            if c >= n:
                break
        ans = max(temp, ans)

print(ans)

Fixed submission:
n, k = map(int, input().split())
v = list(map(int, input().split()))

m = k // 2 + (k % 2)

a = [0] * (n+1)
d = [0] * (n+1)

a[0] = 0
d[0] = 0
for i in range(n):
    a[i+1] = a[i] + v[i]
    d[i+1] = d[i] + v[n-1-i]

w = []
for i in range(n):
    w.append([v[i], i])

w.sort()

ans = 0

p = min(n, k)
for L in range(p + 1):
    for R in range(p + 1 - L):
        #print(L, R)
        temp = a[L] + d[R]
        b = min(L + R, k - L - R)
        c = 0
        while b > 0:
            if w[c][0] >= 0:
                break
            if w[c][1] < L or w[c][1] > n - 1 - R:
                #print(w[c][1], L, R)
                temp -= w[c][0]
                b -= 1
            c += 1
        #print(L, R, temp, b)
        ans = max(ans, temp)

print(ans)

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
v = list(map(int, input().split()))

m = k // 2 + (k % 2)

a = [0] * (n+1)
d = [0] * (n+1)

a[0] = 0
d[n] = 0
for i in range(n):
    a[i+1] = a[i] + v[i]
    d[n-1-i] = d[n-i] + v[n-1-i]

w = []
for i in range(n):
    w.append([v[i], i])

w.sort()

ans = 0

#print(a)
#print(d)

for i in range(m):
    p = k - i
    for j in range(p+1):
        #print(j, n-p-j, a[j], d[n-p-j])
        temp = a[j] + d[n-p+j]
        c = 0
        for _ in range(i):
            if w[c][0] >= 0:
                break
            if w[c][1] < j or w[c][1] > n-p+j:
                temp -= w[c][0]
            c += 1


        ans = max(temp, ans)

print(ans)

Fixed submission:
n, k = map(int, input().split())
v = list(map(int, input().split()))

m = k // 2 + (k % 2)

a = [0] * (n+1)
d = [0] * (n+1)

a[0] = 0
d[0] = 0
for i in range(n):
    a[i+1] = a[i] + v[i]
    d[i+1] = d[i] + v[n-1-i]

w = []
for i in range(n):
    w.append([v[i], i])

w.sort()

ans = 0

p = min(n, k)
for L in range(p + 1):
    for R in range(p + 1 - L):
        #print(L, R)
        temp = a[L] + d[R]
        b = min(L + R, k - L - R)
        c = 0
        while b > 0:
            if w[c][0] >= 0:
                break
            if w[c][1] < L or w[c][1] > n - 1 - R:
                #print(w[c][1], L, R)
                temp -= w[c][0]
                b -= 1
            c += 1
        #print(L, R, temp, b)
        ans = max(ans, temp)

print(ans)

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
v = list(map(int, input().split()))

m = k // 2 + (k % 2)

a = [0] * (n+1)
d = [0] * (n+1)

a[0] = 0
d[n] = 0
for i in range(n):
    a[i+1] = a[i] + v[i]
    d[n-1-i] = d[n-i] + v[n-1-i]

w = []
for i in range(n):
    w.append([v[i], i])

w.sort()

ans = 0

for i in range(m):
    p = k - i
    for j in range(p+1):
        #print(j, n-p-j, a[j], d[n-p-j])
        temp = a[j] + d[n-p+j]
        c = 0
        for _ in range(i):
            if w[c][0] >= 0:
                break
            if w[c][1] < j or w[c][1] > n-p-j:
                temp -= w[c][0]
            c += 1
        ans = max(temp, ans)

print(ans)

Fixed submission:
n, k = map(int, input().split())
v = list(map(int, input().split()))

m = k // 2 + (k % 2)

a = [0] * (n+1)
d = [0] * (n+1)

a[0] = 0
d[0] = 0
for i in range(n):
    a[i+1] = a[i] + v[i]
    d[i+1] = d[i] + v[n-1-i]

w = []
for i in range(n):
    w.append([v[i], i])

w.sort()

ans = 0

p = min(n, k)
for L in range(p + 1):
    for R in range(p + 1 - L):
        #print(L, R)
        temp = a[L] + d[R]
        b = min(L + R, k - L - R)
        c = 0
        while b > 0:
            if w[c][0] >= 0:
                break
            if w[c][1] < L or w[c][1] > n - 1 - R:
                #print(w[c][1], L, R)
                temp -= w[c][0]
                b -= 1
            c += 1
        #print(L, R, temp, b)
        ans = max(ans, temp)

print(ans)