Problem:


Score : 400 points

Problem Statement

Your friend gave you a dequeue D as a birthday present.

D is a horizontal cylinder that contains a row of N jewels.

The values of the jewels are V_1, V_2, ..., V_N from left to right. There may be jewels with negative values.

In the beginning, you have no jewel in your hands.

You can perform at most K operations on D, chosen from the following, at most K times (possibly zero):

Operation A: Take out the leftmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

Operation B: Take out the rightmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

Operation C: Choose a jewel in your hands and insert it to the left end of D. You cannot do this operation when you have no jewel in your hand.

Operation D: Choose a jewel in your hands and insert it to the right end of D. You cannot do this operation when you have no jewel in your hand.

Find the maximum possible sum of the values of jewels in your hands after the operations.

Constraints

All values in input are integers.

1 \leq N \leq 50

1 \leq K \leq 100

-10^7 \leq V_i \leq 10^7

Input

Input is given from Standard Input in the following format:

N K
V_1 V_2 ... V_N

Output

Print the maximum possible sum of the values of jewels in your hands after the operations.

Sample Input 1

6 4
-10 8 2 1 2 6

Sample Output 1

14

After the following sequence of operations, you have two jewels of values 8 and 6 in your hands for a total of 14, which is the maximum result.

Do operation A. You take out the jewel of value -10 from the left end of D.

Do operation B. You take out the jewel of value 6 from the right end of D.

Do operation A. You take out the jewel of value 8 from the left end of D.

Do operation D. You insert the jewel of value -10 to the right end of D.

Sample Input 2

6 4
-6 -100 50 -2 -5 -3

Sample Output 2

44

Sample Input 3

6 3
-6 -100 50 -2 -5 -3

Sample Output 3

0

It is optimal to do no operation.



Sample input:
6 4
-10 8 2 1 2 6


Sample output:
14

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
from collections import deque
import heapq


def iip(listed):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret


def main():
    r = solve()
    print(r)


def solve():
    N, K = iip(False)
    V = iip(True)
    V2 = [i for i in V]
    V2.reverse()

    mm = 0
    for i in range(N):
        for j in range(N):
            if i+j > K:
                continue
            if i+j > N:
                continue
            jewels = V[:i]
            jewels.extend(V2[:j])

            zyokyo = K - (i+j)

            while zyokyo >= 1:
                zyokyo -= 1
                if not jewels:
                    break
                mj = min(jewels)
                if min(jewels) < 0:
                    jewels.remove(mj)
                else:
                    break

            mm = max(mm, sum(jewels))
    return mm


#####################################################ライブラリ集ここから

def split_print_space(s):
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):
    print("\n".join([str(i) for i in s]))


def koenai_saidai_x_index(sorted_list, n):
    l = 0
    r = len(sorted_list)
    if len(sorted_list) == 0:
        return False
    if sorted_list[0] > n:
        return False

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] == n:
            return x
        elif sorted_list[x] > n:
            r = x
        else:
            l = x
    return l


def searchsorted(sorted_list, n, side):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    if n > sorted_list[-1]:
        # print(sorted_list)
        return len(sorted_list)
    if n < sorted_list[0]:
        return 0

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        if sorted_list[l] == n:
            return r - 1
        else:
            return r

    if side == "right":
        if sorted_list[l] == n:
            return l + 1
        else:
            return l


def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):
    return power(n, mod - 2)


def power(n, p):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2) ** 2) % mod
    if p % 2 == 1:
        return (n * power(n, p - 1)) % mod


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
r = -10**18
for i in range(-1, N):
    for j in range(i+1, N+1):
        A, B = X[:i+1], X[j:]
        if i+1+N-j > K:
            continue
#        print(i, j, A, B, i+1,N-j)
        C = sorted(A+B)[::-1]
        for i in range(K-(i+1+N-j)):
            if not C:
                break
            c = C.pop()
            if c > 0:
                C.append(c)
                break
#        print(C)
        r = max(r, sum(C))
print(r)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
r = -10**18
for i in range(-1, N):
    for j in range(i+1, N+1):
        A, B = X[:i+1], X[j:]
        if i+1+N-j > K:
            continue
#        print(i, j, A, B, i+1,N-j)
        C = sorted(A+B)[::-1]
        for i in range(K-(i+1+N-j)):
            if not C:
                break
            C.pop()
#        print(C)
        r = max(r, sum(C))
print(r)

Submission status: Wrong Answer

Solution code:
from collections import deque
import heapq

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


#a : heapq.heappop(s,v.popleft())
#b : heapq.heappop(s,v.pop())
#c : v.appendleft(heapq.heappop(s))
#d : v.append(heapq.heappop(s))
ans = 0
for i in range(1,min(n,k)+1):
    s = []
    heapq.heapify(s)
    for j in range(i+1):
        res = 0
        num = 0
        for _ in range(j):
            heapq.heappush(s,v[num])
            res += v[num]
            num += 1
        num = 1
        for _ in range(j,i):
            heapq.heappush(s,v[-num])
            res += v[-num]
            num += 1

        for j in range(min(n,k)-i):
            if len(s) == 0:
                break
            if s[0] < 0:
                res -= heapq.heappop(s)
        if ans < res:
            ans = res
    #print(i,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import heapq

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


#a : heapq.heappop(s,v.popleft())
#b : heapq.heappop(s,v.pop())
#c : v.appendleft(heapq.heappop(s))
#d : v.append(heapq.heappop(s))
ans = 0
for i in range(1,min(n,k)+1):
    s = []
    heapq.heapify(s)
    for j in range(i+1):
        res = 0
        num = 0
        for _ in range(j):
            heapq.heappush(s,v[num])
            res += v[num]
            num += 1
        num = 1
        for _ in range(j,i):
            heapq.heappush(s,v[-num])
            res += v[-num]
            num += 1

        for j in range(k-i):
            if len(s) == 0:
                break
            if s[0] < 0:
                res -= heapq.heappop(s)
        if ans < res:
            ans = res
    #print(i,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

def main():
    N, K = [int(i) for i in input().split()]
    v = np.array([int(i) for i in input().split()])
    res = 0
    max_ext = min(K, N)
    for a in range(0, max_ext+1):
        v_a = v[:a] 
        for b in range(0, max_ext-a):
            v_ext = v_a
            if b > 0:
                v_ext = np.hstack([v_ext, v[-b:]])
            v_ext = np.sort(np.array(v_ext))
            max_rev = K - (a+b)
            num_negative = (v_ext < 0).sum()
            if max_rev > num_negative:
                t_res = v_ext[num_negative:].sum()
            else:
                t_res = v_ext[max_rev:].sum()
            res = max(res, t_res) 
    print(res)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

def main():
    N, K = [int(i) for i in input().split()]
    v = [int(i) for i in input().split()]
    res = 0
    for k in range(0, K+1):
        for a in range(k+1):
            v_ext = v[:a] 
            if k - a > 0 and N > a:
                v_ext += v[-(k-a):]
            v_ext = np.sort(np.array(v_ext))
            max_rev = K - k
            num_negative = (v_ext < 0).sum()
            if max_rev > num_negative:
                t_res = v_ext[num_negative:].sum()
            else:
                t_res = v_ext[max_rev:].sum()
            res = max(res, t_res) 
    print(res)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

def main():
    N, K = [int(i) for i in input().split()]
    v = [int(i) for i in input().split()]
    res = 0
    for k in range(0, K+1):
        for a in range(k+1):
            v_ext = v[:a] 
            if a < k:
                v_ext += v[-(k-a):]
            v_ext = np.sort(np.array(v_ext))
            max_rev = K - k
            num_negative = (v_ext < 0).sum()
            if max_rev > num_negative:
                t_res = v_ext[num_negative:].sum()
            else:
                t_res = v_ext[max_rev:].sum()
            res = max(res, t_res) 
    print(res)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def resolve():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    ans=0
    minus=0
    for i in range(k+1):
        for j in range(i+1):
            for t in range(j+1):
                w=sorted(v[:j-t] + v[n -t:])
                for l in w[:i-j]:
                    if l<0:
                       minus+=l
                ans=max(ans,(sum(w)-minus))
                minus=0
    print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
V = list(map(int, input().split()))

ops = min(N,K)

ans = 0
for A in range(0,ops+1):
    l = V[:A]
    l.sort()
    for B in range(0,ops+1-A):
        ll = l[:]
        if B > 0:
            ll = l + V[-B:]
            ll.sort()
        for i in range(0,min(K-(A+B),A+B)):
            if ll[i] < 0:
                ll[i] = 0
            else:
                break
        print(A,B,ll)
        ans = max(sum(ll),ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
V = list(map(int, input().split()))

ops = min(N,K)

ans = 0
for A in range(ops+1):
    for B in range(ops+1-A):
        summ = 0
        l = sorted(V[0:A] + V[N-B:N])
        summ = sum(l)
        for i in range(min(ops-(A+B),len(l))):
            if l[i] < 0:
                summ -= l[i]
        ans = max(summ,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
V = list(map(int, input().split()))

ops = min(N,K)

ans = 0
for A in range(ops+1):
    for B in range(ops+1-A):
        l = sorted(V[0:A] + V[N-B:N])
        summ = sum(l)
        for i in range(min(ops-(A+B),len(l))):
            if l[i] < 0:
                summ -= l[i]
        ans = max(summ,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
V = list(map(int, input().split()))

ops = min(N,K)

ans = 0
for A in range(ops+1):
    for B in range(ops+1-A):
        summ = 0
        l = sorted(V[0:A] + V[N-B:N])
        summ = sum(l)
        for i in range(min(ops-(A+B),A+B)):
            if l[i] < 0:
                summ -= l[i]
        ans = max(summ,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
V = list(map(int, input().split()))

ops = min(N,K)

ans = 0
for A in range(ops+1):
    for B in range(ops+1-A):
        l = sorted(V[0:A] + V[N-B:N])
        summ = sum(l)
        for i in range(min(ops-(A+B),A+B)):
            if l[i] < 0:
                summ -= l[i]
        ans = max(summ,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
def resolve():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    ans=0
    minus=0
    for i in range(k+1):
        for j in range(i+1):
            for t in range(j+1):
                w=sorted(v[:j-t] + v[n -t:])
                for l in w[:i-j]:
                    if l<0:
                       minus+=l
                       ans=max(ans,(sum(w)-minus))
                minus=0
    print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
V = list(map(int, input().split()))

ops = min(N,K)

ans = 0
for A in range(ops+1):
    for B in range(ops+1-A):
        l = V[0:A] + V[N-B:N]
        l.sort()
        comp = min(ops-(A+B),A+B)
        minus = 0
        for i in range(comp):
            if l[i] < 0:
                minus -= l[i]
            else:
                break
        ans = max(sum(l)+minus,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
V = list(map(int, input().split()))

ops = min(N,K)

ans = 0
for A in range(ops+1):
    for B in range(ops+1-A):
        l = sorted(V[:A] + V[N-B:N])
        comp = min(ops-(A+B),A+B)
        summ = sum(l)
        for i in range(comp):
            if l[i] < 0:
                summ -= l[i]
        ans = max(summ,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
V = list(map(int, input().split()))

ops = min(N,K)

ans = 0
for A in range(ops+1):
    for B in range(ops+1-A):
        l = V[:A] + V[N-B:N]
        l.sort()
        comp = min(ops-(A+B),A+B)
        summ = sum(l)
        for i in range(comp):
            if l[i] < 0:
                summ -= l[i]
        ans = max(summ,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
V = list(map(int, input().split()))

ops = min(N,K)

ans = 0
for A in range(ops+1):
    for B in range(ops+1-A):
        l = V[:A] + V[N-B:N]
        l.sort()
        comp = min(ops-(A+B),A+B)
        summ = sum(l)
        for i in range(comp):
            if l[i] < 0:
                summ -= l[i]
            else:
                break
        ans = max(summ,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
V = list(map(int, input().split()))

ops = min(N,K)

ans = 0
for A in range(0,ops+1):
    for B in range(0,ops+1-A):
        l = V[:A] + V[N-B:N]
        l.sort()
        comp = min(ops-(A+B),A+B)
        minus = 0
        for i in range(comp):
            if l[i] < 0:
                minus -= l[i]
            else:
                break
        ans = max(sum(l)+minus,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
V = list(map(int, input().split()))

ops = min(N,K)

ans = 0
for A in range(0,ops+1):
    l = V[:A]
    l.sort()
    for B in range(0,ops+1-A):
        ll = l[:]
        if B > 0:
            ll += V[-B:]
            ll.sort()
        comp = min(ops-(A+B),A+B)
        minus = 0
        if comp > 0:
            for i in range(0,comp):
                if ll[i] < 0:
                    minus -= ll[i]
                else:
                    break
        ans = max(sum(ll)+minus,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
V = list(map(int, input().split()))

ops = min(N,K)

ans = 0
for A in range(0,ops+1):
    l = V[:A]
    l.sort()
    for B in range(0,ops+1-A):
        ll = l
        if B > 0:
            ll = l + V[-B:]
            ll.sort()
        comp = min(ops-(A+B),A+B)
        minus = 0
        if comp > 0:
            for i in range(0,comp):
                if ll[i] < 0:
                    minus -= ll[i]
                else:
                    break
        ans = max(sum(ll)+minus,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
V = list(map(int, input().split()))

ops = min(N,K)

ans = 0
for A in range(0,ops+1):
    l = V[:A]
    l.sort()
    for B in range(max(0,(ops+1)//2-A),ops+1-A):
        ll = l
        if B > 0:
            ll = l + V[-B:]
            ll.sort()
        ans = max(sum(ll[ops-(A+B):]),ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
V = list(map(int, input().split()))

ops = min(N,K)

ans = 0
for A in range(0,ops+1):
    l = V[:A]
    l.sort()
    for B in range(max(0,(ops+1)//2-A),ops+1-A):
        ll = l
        if B > 0:
            ll = l + V[-B:]
            ll.sort()
        print(A,B,ll,ll[ops-(A+B):])
        ans = max(sum(ll[ops-(A+B):]),ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
V = list(map(int, input().split()))

ops = min(N,K)

ans = 0
for A in range(0,ops+1):
    l = V[:A]
    l.sort()
    for B in range(0,ops+1-A):
        ll = l
        if B > 0:
            ll = l + V[-B:]
            ll.sort()
        ans = max(sum(ll[ops-(A+B):]),ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
take_chance = min(N,K)
ans = 0
for A in range(take_chance+1):
    for B in range(take_chance-A+1):
        sum1 = 0
        take = sorted(V[0:A] + V[N-B:N], reverse = True)
        suteru_chance = K-(A+B)
        for i in range(len(take)-suteru_chance):
            #if(take[i]<0):
                #break
            sum1 += take[i]
        ans = max(ans,sum1)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int,input().split()))

ans = -1 * float("INF")
for l in range(K):
  for r in range(K-l):
    if l + r > N:
      break
    d = K - l - r
    s = V[0:l] + V[N-1:N-1-r:-1]
    value = sum(s)
    s.sort()
    for i in range(d):
      if i >= len(s):
        break
      if s[i] < 0:
        value -= s[i]
      else:
        break
    ans = max(ans, value)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int,input().split()))

ans = 0
for l in range(K):
  for r in range(K-l):
    if l + r > N:
      break
    d = K - l - r
    s = V[0:l] + V[N-1:N-1-r:-1]
    value = sum(s)
    s.sort()
    for i in range(d):
      if i >= len(s):
        break
      if s[i] < 0:
        value -= s[i]
      else:
        break
    ans = max(ans, value)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))
ans=-10**18
for l in range(n+1):
  for r in range(n+1):
    if l+r>n or l+r>k:
      continue
    trg=v[:l]+v[n-r:]
    trg.sort(reverse=True)
    ans=max(ans,sum(trg[:len(trg)-k+l+r]))
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
n, k = map(int, input().split())
V = list(map(int, input().split()))
max_dequeue = min(n, k)

max_value = 0
for dequeue_num in range(max_dequeue):
  for left in range(dequeue_num + 1):
    right = dequeue_num - left
    #print(left, right, dequeue_num)
    jewel_taken = sorted(V[:left] + V[n - right:])
    #print(jewel_taken)
    pos_zero = bisect.bisect(jewel_taken, 0)
    ret_num = min(k - dequeue_num, pos_zero)
    #print(pos_zero, k - dequeue_num)
    #print(jewel_taken[ret_num:])
    #print()
    max_value = max(max_value, sum(jewel_taken[ret_num:]))
print(max_value)

Submission status: Wrong Answer

Solution code:
import bisect
n, k = map(int, input().split())
V = list(map(int, input().split()))
max_dequeue = min(n, k)

max_value = 0
for dequeue_num in range(max_dequeue):
  for left in range(dequeue_num + 1):
    right = dequeue_num - left
    #print(left, right, dequeue_num)
    jewel_taken = sorted(V[:left] + V[n - right:])
    #print(jewel_taken)
    pos_zero = bisect.bisect(jewel_taken, 0)
    ret_num = min(max_dequeue - dequeue_num, pos_zero)
    max_value = max(max_value, sum(jewel_taken[ret_num:]))
print(max_value)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n,k = list(map(int,input().split()))
v = list(map(int,input().split()))

#左からl個、右からr個取り出し、k-l-r個まで捨てる
ans = 0
for l in range(k):
    for r in range(k-l):
        if l+r>n:
            continue
        d = k-l-r
        cur_list = v[0:l]+v[n-r:n]
        cur_list.sort()
        cur_ret = sum(cur_list)
        for di in range(min(d,len(cur_list))):
            if cur_list[di] >= 0:
                break
            cur_ret-=cur_list[di]
        ans = max(ans,cur_ret)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n,k = list(map(int,input().split()))
v = list(map(int,input().split()))

#左からl個、右からr個取り出し、k-l-r個まで捨てる
ans = 0
for l in range(k):
    for r in range(k):
        if l+r>n or l+r>k:
            break
        cur_ret = 0
        d = k-l-r
        cur_list = v[0:l]+v[n-r:n]
        cur_list.sort()
        cur_ret = sum(cur_list)
        for di in range(min(d,len(cur_list))):
            if cur_list[di] >= 0:
                break
            cur_ret-=cur_list[di]
        ans = max(ans,cur_ret)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n,k = list(map(int,input().split()))
v = list(map(int,input().split()))

#左からl個、右からr個取り出し、k-l-r個まで捨てる
ans = 0
for l in range(k):
    for r in range(k):
        if l+r>n:
            break
        cur_ret = 0
        d = k-l-r
        cur_list = v[0:l]+v[n-r:n]
        cur_list.sort()
        cur_ret = sum(cur_list)
        for di in range(min(d,len(cur_list))):
            if cur_list[di] >= 0:
                break
            cur_ret-=cur_list[di]
        ans = max(ans,cur_ret)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n,k = list(map(int,input().split()))
v = list(map(int,input().split()))

#左からl個、右からr個取り出し、k-l-r個まで捨てる
ans = 0
for l in range(k):
    for r in range(k):
        if l+r>n:
            break
        cur_ret = 0
        d = k-l-r
        cur_list = v[0:l]+v[n-r:n]
        cur_list.sort()
        cur_ret = sum(cur_list)
        for di in range(min(d,l+r)):
            if cur_list[di] >= 0:
                break
            cur_ret-=cur_list[di]
        ans = max(ans,cur_ret)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n,k = list(map(int,input().split()))
v = list(map(int,input().split()))

#左からl個、右からr個取り出し、k-l-r個まで捨てる
ans = 0
for l in range(k):
    for r in range(k-l):
        if l+r>n:
            break
        cur_ret = 0
        d = k-l-r
        cur_list = v[0:l]+v[n-r:n]
        cur_list.sort()
        cur_ret = sum(cur_list)
        for di in range(min(d,l+r)):
            if cur_list[di] >= 0:
                break
            cur_ret-=cur_list[di]
        ans = max(ans,cur_ret)
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
n, k = map(int, input().split())
v = list(map(int, input().split()))

result = []
for i in range(min(n, k)):
    for j in range(k-i):
        x = v[:i]+v[n-j:]
        c = k - (i + j)
        x.sort()
        for t in range(min(len(x), c)):
            if x[0]<0:
                x.pop(0)
            else:
                break
        result.append(sum(x))
print(max(result))

Submission status: Wrong Answer

Solution code:
import heapq
n, k = map(int, input().split())
v = list(map(int, input().split()))

result = []
for i in range(min(n, k)):
    for j in range(k-i+1):
        x = v[:i]+v[n-j:]
        c = k - (i + j)
        x.sort()
        for t in range(min(len(x), c)):
            if x[0]<0:
                x.pop(0)
            else:
                break
        result.append(sum(x))
print(max(result))

Submission status: Wrong Answer

Solution code:
import heapq
n, k = map(int, input().split())
v = list(map(int, input().split()))

result = []
for i in range(min(n, k)):
    for j in range(k-i):
        x = v[:i]+v[n-j:]
        c = i + j
        x.sort()
        while len(x)>0 and x[0] < 0 and c < k:
            x.pop(0)
            c += 1
        result.append(sum(x))
print(max(result))

Submission status: Wrong Answer

Solution code:
import heapq
n, k = map(int, input().split())
v = list(map(int, input().split()))

result = []
for i in range(min(n, k)):
    for j in range(k-i):
        x = v[:i]+v[n-j:]
        heapq.heapify(x)
        c = i + j
        while len(x)>0 and x[0] < 0 and c < k:
            heapq.heappop(x)
            c += 1
        result.append(sum(x))
print(max(result))

Submission status: Wrong Answer

Solution code:
from collections import deque
import heapq
import copy
n, k = map(int, input().split())
v = deque(list(map(int, input().split())))

result = [0]
for i in range(k+1):
    for j in range(k-i):
        jue = copy.deepcopy(v)
        p = []
        heapq.heapify(p)
        ii = 0
        while len(jue)!=0 and ii < i:
            heapq.heappush(p, jue.pop())
            ii += 1
        jj = 0
        while len(jue)!=0 and jj < j:
            heapq.heappush(p, jue.popleft())
            jj += 1
        ij = ii+jj
        while len(p)!=0 and ij <= k:
            x = heapq.heappop(p)
            if x > 0:
                heapq.heappush(p, x)
                ij += 10**5
            ij += 1
        result.append(sum(p))

print(max(result))

Submission status: Wrong Answer

Solution code:
from collections import deque
import heapq
import copy
n, k = map(int, input().split())
v = deque(list(map(int, input().split())))

result = [0]
for i in range(k+1):
    for j in range(k-i):
        jue = copy.deepcopy(v)
        p = []
        heapq.heapify(p)
        ii = 0
        while len(jue)!=0 and ii < i:
            heapq.heappush(p, jue.pop())
            ii += 1
        jj = 0
        while len(jue)!=0 and jj < j:
            heapq.heappush(p, jue.popleft())
            jj += 1
        ij = ii+jj
        while len(p)!=0 and ij < k:
            x = heapq.heappop(p)
            if x > 0:
                heapq.heappush(p, x)
                ij += 10**5
            ij += 1
        result.append(sum(p))

print(max(result))

Submission status: Wrong Answer

Solution code:
from collections import deque
import heapq
import copy
n, k = map(int, input().split())
v = deque(list(map(int, input().split())))

result = [0]
for i in range(k+1):
    for j in range(k-i):
        jue = copy.deepcopy(v)
        p = []
        heapq.heapify(p)
        ii = 0
        while len(jue)!=0 and ii < i:
            heapq.heappush(p, jue.pop())
            ii += 1
        jj = 0
        while len(jue)!=0 and jj < j:
            heapq.heappush(p, jue.popleft())
            jj += 1
        ij = ii+jj
        while len(p)!=0 and ij < k:
            x = heapq.heappop(p)
            if x > 0:
                heapq.heappush(p, x)
                break
            ij += 1
        result.append(sum(p))

print(max(result))

Submission status: Wrong Answer

Solution code:
from collections import deque
import heapq
import copy
n, k = map(int, input().split())
v = deque(list(map(int, input().split())))

result = [0]
for i in range(k):
    for j in range(k-i):
        jue = copy.deepcopy(v)
        p = []
        heapq.heapify(p)
        ii = 0
        while len(jue)!=0 and ii < i:
            heapq.heappush(p, jue.pop())
            ii += 1
        jj = 0
        while len(jue)!=0 and jj < j:
            heapq.heappush(p, jue.popleft())
            jj += 1
        ij = ii+jj
        while len(p)!=0 and ij < k:
            x = heapq.heappop(p)
            if x > 0:
                heapq.heappush(p, x)
                break
            ij += 1
        result.append(sum(p))

print(max(result))

Submission status: Wrong Answer

Solution code:
from copy import deepcopy
from collections import deque
from functools import partial, lru_cache
from heapq import heappush, heappop

inputs = open(0).readlines()
n, k = map(int, inputs[0].split())
V = deque(map(int, inputs[1].split()))

def dfs(root, option, function):
    stack =[root]
    ans = 0
    while stack:
        current_node = stack.pop()
        ans = function(current_node, ans)
        for n in option(current_node):
            stack.append(n)
    return ans

def option(node, limit):
    i, j = node
    if i+j < limit:
        return ((i+1, j), (i, j+1))
    return tuple()

def function(node, ans, left_memo, right_memo, limit, memo):
    if node in memo:
        return max(ans, memo[node])
    i, j = node
    W = list(left_memo[i])
    W.extend(list(right_memo[j]))
    score = sum(W)
    for _ in range(min(limit-i-j, i+j)):
        if W[0] < 0:
            score -= heappop(W)
    memo[node] = score
    return max(ans, score)

def get_left(i, V):
    W, V = [], deepcopy(V)
    for _ in range(i):
        heappush(W, V.popleft())
    return tuple(W)
left_memo = {i:get_left(i, V) for i in range(min(n, k)+1)}

def get_right(i, V):
    W, V = [], deepcopy(V)
    for _ in range(i):
        heappush(W, V.pop())
    return tuple(W)
right_memo = {i:get_right(i, V) for i in range(min(n, k)+1)}

ans = dfs((0, 0), partial(option, limit=min(n, k)), partial(function, left_memo=left_memo, right_memo=right_memo, limit=k, memo={}))
print(ans)

Submission status: Wrong Answer

Solution code:
from copy import deepcopy
from collections import deque
from functools import partial, lru_cache
from heapq import heappush, heappop

inputs = open(0).readlines()
n, k = map(int, inputs[0].split())
V = deque(map(int, inputs[1].split()))
W = []

n, k = map(int, inputs[0].split())
V = deque(map(int, inputs[1].split()))
W = []


def dfs(root, option, function):
    nodes, stack = [], [root]
    ans = 0
    while stack:
        current_node = stack.pop()
        if current_node in nodes:
            continue
        ans = function(current_node, ans)
        for n in option(current_node):
            if n not in nodes:
                stack.append(n)
    return ans

def option(node, k):
    i, j = node
    if i+j < k:
        return ((i+1, j), (i, j+1))
    return tuple()

def function(node, ans, W, V, k, memo):
    if node in memo:
        return max(ans, memo[node])
    i, j = node
    W_, V_ = map(deepcopy, (W, V))
    for _ in range(i):
        if V_:
            heappush(W_, V_.pop())
    for _ in range(j):
        if V_:
            heappush(W_, V_.popleft())
    for _ in range(k-i-j):
        if W_:
            _ = heappop(W_)
    memo[node] = sum(W_)
    return max(ans, memo[node])

ans = dfs((0, 0), partial(option, k=k), partial(function, W=W, V=V, k=k, memo={}))
print(ans)