Problem p03032 (python)

Fixed submission:
n,k = map(int,input().split())
v = list(map(int,input().split()))
ansl = []
for i in range(min(k+1,n+1)):
    for j in range(i+1):
        v1 = v[0:j]
        v2 = v[n-(i-j):n]    
        v1.extend(v2)
        v1.sort()
        for x in range(k-i):
            if len(v1) != 0 and v1[0] < 0:
                del v1[0]
            else:
                break
        if len(v1) != 0:
            ansl.append(sum(v1))
        else:
            ansl.append(0)
ans = max(ansl)
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
v = list(map(int,input().split()))
ansl = []
p = 1
for i in range(min(k+1,n+1)):
    for j in range(i+1):
        v1 = v[0:j]
        v2 = v[n-(i-j):n]    
        v1.extend(v2)
        v1.sort()
        for x in range(k-i):
            if len(v1) != 0:
                del v1[0]
        if len(v1) != 0:
            ansl.append(sum(v1))
        elif len(v1) == 0 and k%2 == 0:
            ansl.append(0)
ans = max(ansl)
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
v = list(map(int,input().split()))
ansl = []
p = 1
for i in range(min(k+1,n+1)):
    for j in range(i+1):
        v1 = v[0:j]
        v2 = v[n-(i-j):n]    
        v1.extend(v2)
        v1.sort()
        for x in range(k-i-2):
            if len(v1) != 0:
                del v1[0]
        if len(v1) != 0:
            ansl.append(sum(v1))
        elif len(v1) == 0 and k%2 == 0:
            ansl.append(0)
ans = max(ansl)
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
v = list(map(int,input().split()))
if n <= k:
    ansl = []
    count = 0
    for i in range(n+1):
        for j in range(i+1):
            v1 = v[0:j]
            v2 = v[n-(i-j):n]
            v1.extend(v2)
            v1.sort()
            for x in range(k-i):
                if len(v1) != 0:
                    del v1[0]
            ansl.append(sum(v1))
    ans = max(ansl)
else:
    ansl = []
    for x in range(k+1):
        v0 = []
        v1 = v[0:x]
        v2 = v[n-(k-x):n]
        v3 = sorted(v1)
        v4 = sorted(v2)
        p = 0
        r = 0
        if len(v3) > 0:
            while v3[p] < 0 and len(v1) > 0:
                if v3[p] + v1[-1] < 0:
                    v1.remove(v3[p])
                    del v3[p]
                    if len(v1) > 0:
                        del v1[-1]
                else:
                    p = p + 1
                if p == x-1:
                    break
        if len(v4) > 0:
            while v4[r] < 0 and len(v2) > 0:
                if v4[r] + v2[0] < 0:
                    v2.remove(v4[r])
                    del v4[r]
                    if len(v2) > 0:
                        del v2[0]
                else:
                    r = r + 1
                if r == k - x-1:
                    break
        ansl.append(sum(v1)+sum(v2))
    ans = max(ansl)
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
v = list(map(int,input().split()))
if n <= k:
    ans = sum(v)
    v.sort()
    count = 0
    v0 = []
    for i in range(k-n):
        if v[i] < 0:
            ans = ans - v[i]
            count = count + 1
            v0.append(v[i])
        if (k - count)%2 == 1:
            if len(v0) == 0:
                ans = ans - min(v)
            elif len(v0) > 0 and v[count] > -min(v0):
                ans = ans + min(v0)
            elif len(v0) > 0 and v[count] <= -min(v0):
                ans = ans - v[count]
else:
    ansl = []
    for x in range(k+1):
        v0 = []
        v1 = v[0:x]
        v2 = v[n-(k-x):n]
        v3 = sorted(v1)
        v4 = sorted(v2)
        p = 0
        r = 0
        if len(v3) > 0:
            while v3[p] < 0 and len(v1) > 0:
                if v3[p] + v1[-1] < 0:
                    v1.remove(v3[p])
                    del v3[p]
                    if len(v1) > 0:
                        del v1[-1]
                else:
                    p = p + 1
                if p == x-1:
                    break
        if len(v4) > 0:
            while v4[r] < 0 and len(v2) > 0:
                if v4[r] + v2[0] < 0:
                    v2.remove(v4[r])
                    del v4[r]
                    if len(v2) > 0:
                        del v2[0]
                else:
                    r = r + 1
                if r == k - x-1:
                    break
        ansl.append(sum(v1)+sum(v2))
    ans = max(ansl)
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
v = list(map(int,input().split()))
if n <= k:
    ans = sum(v)
    v.sort()
    count = 0
    v0 = []
    for i in range(k-n):
        if v[i] < 0:
            ans = ans - v[i]
            count = count + 1
            v0.append(v[i])
        if (k - count)%2 == 1:
            if len(v0) == 0:
                ans = ans - min(v)
            elif len(v0) > 0 and v[count] > -min(v0):
                ans = ans + min(v0)
            elif len(v0) > 0 and v[count] <= -min(v0):
                ans = ans - v[count]
else:
    ansl = []
    for x in range(k+1):
        v0 = []
        v1 = v[0:x]
        v2 = v[n-(k-x):n]
        v3 = sorted(v1)
        v4 = sorted(v2)
        p = 0
        r = 0
        if len(v3) > 0:
            while v3[p] < 0 and len(v1) > 0:
                if v3[p] + v1[-1] < 0:
                    v1.remove(v3[p])
                    del v3[p]
                    if len(v1) > 0:
                        del v1[-1]
                else:
                    p = p + 1
                if p == x-1:
                    break
        if len(v4) > 0:
            while v4[r] < 0 and len(v2) > 0:
                if v4[r] + v2[0] < 0:
                    v2.remove(v4[r])
                    del v4[r]
                    if len(v2) > 0:
                        del v2[0]
                else:
                    r = r + 1
                if r == k - x-1:
                    break
        ansl.append(sum(v1)+sum(v2))
    ans = max(ansl)
print(ans)