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:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,K=map(int,input().split())
    V=list(map(int,input().split()))
    ans=0
    for i in range(K//2):
        for j in range(K-i+1):
            l=V[:j]+V[N-(K-i-j):]
            l.sort()
            for k in range(i+1):
                ans=max(ans,sum(l[k:]))
    print(ans)
            
        

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,K=map(int,input().split())
    V=list(map(int,input().split()))
    ans=0
    for i in range(K//2):
        for j in range(K-i+1):
            l=V[:j]+V[N-(K-i-j):]
            l.sort()
            ans=max(ans,sum(l[i:]))
    print(ans)
            
        

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,K=map(int,input().split())
    V=list(map(int,input().split()))
    ans=0
    for i in range(min(50,K-1)):
        for j in range(K-i+1):
            l=V[:j]+V[N-(K-i-j):]
            l.sort()
            ans=max(ans,sum(l[min(i,N-i-1):]))
    print(ans)
            
        

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
    N,K = map(int,input().split())
    V = list(map(int,input().split()))
    
    if N < K:
        K = N
    ans = -(10**10)
    for pick in range(K+1):
        throw = min(pick,K-pick)
        for left in range(pick+1):
            right = pick-left
            p = []
            minus = 0
            for i in range(left):
                if V[i] < 0:
                    minus += 1
                p.append(V[i])
            for i in range(right):
                if V[-i-1] < 0:
                    minus += 1
                p.append(V[-i-1])
            p.sort()
            if minus >= throw:
                rest = p[throw:]
            else:
                rest = p[minus:]
            ans = max(ans,sum(rest))
    
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

# 最終的に「持っているもの」の価値の合計を高めたい
# Dから取り出すことをpop、Dに詰めることをenqueueとする
ans = 0
for pop_cnt in range(K + 1):
    enqueue_cnt = K - pop_cnt
    if (pop_cnt > N) or (enqueue_cnt > pop_cnt):
        continue

    for l in range(pop_cnt + 1):
        holding = []

        r = pop_cnt - l
        holding += V[:l]
        holding += V[N - r:]

        holding.sort()
        tmp_ans = sum(holding)

        for e in range(enqueue_cnt + 1):
            if e >= len(holding):
                break

            if holding[e] < 0:
                tmp_ans += -holding[e]

        ans = max(ans, tmp_ans)

print(ans)

Submission status: Wrong Answer

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

# 最終的に「持っているもの」の価値の合計を高めたい
# Dから取り出すことをpop、Dに詰めることをenqueueとする
ans = 0
for pop_cnt in range(K + 1):
    enqueue_cnt = K - pop_cnt
    if (pop_cnt > N) or (enqueue_cnt > pop_cnt):
        continue

    for l in range(pop_cnt + 1):
        holding = []

        r = pop_cnt - l
        holding += V[:l]
        holding += V[N - r:]

        holding.sort()
        tmp_ans = sum(holding)

        for e in range(enqueue_cnt):
            if e >= len(holding):
                break

            if holding[e] < 0:
                tmp_ans += -holding[e]

        ans = max(ans, tmp_ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
n,k=map(int, input().split())
V=list(map(int, input().split()))
ans=0
for i in range(k+1):
  for j in range(k+1-i):
    if i+j>n:
      break  
    #手に取りだしたもの
    A=sorted(V[:i]+V[n-j:])
    a=min(n-i-j,bisect.bisect_left(A, 0))
    ans=max(ans,sum(A[a:]))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
M = min(N, K)
ans = 0
for i in range(M+1):
    for a in range(i+1):
        L = sorted(V[:a] + V[N-(i-a):], reverse=True)
        for _ in range(M-i):
            if L and L[-1] < 0:
                L.pop()
            else:
                break
        ans = max(ans, sum(L))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys


def main():
    # 読み込み回数が多い場合
    input = sys.stdin.readline
    # 複数
    N, K = map(int, input().split())

    # リスト読み込み
    V_A = [inp for inp in map(int, input().split())]
    V_B = V_A.copy()[::-1]
    
    ret = 0
    for A in range(min(K + 1, N + 1)):
        for B in range(min(K - A + 1, N + 1)):
            left_jewels = V_A.copy()[:A]
            right_jewels = V_B.copy()[:B]

            if len(left_jewels) >= len(right_jewels):
                jewels = left_jewels
                jewels.extend(right_jewels)
            else:
                jewels = right_jewels
                jewels.extend(left_jewels)
            jewels.sort()

            score = sum(jewels)

            for i in range(min(K - A - B, len(jewels))):
                jewel = jewels[i]
                if jewel < 0:
                    score -= jewel
                else:
                    break
                    
            ret = max(ret, score)
                
    print(ret)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys


def main():
    # 読み込み回数が多い場合
    input = sys.stdin.readline
    # 複数
    N, K = map(int, input().split())

    # リスト読み込み
    V_A = [inp for inp in map(int, input().split())]
    V_B = V_A.copy()[::-1]
    
    ret = 0
    for A in range(K + 1):
        for B in range(K - A + 1):
            left_jewels = V_A.copy()[:A]
            right_jewels = V_B.copy()[:B]

            if len(left_jewels) >= len(right_jewels):
                jewels = left_jewels
                jewels.extend(right_jewels)
            else:
                jewels = right_jewels
                jewels.extend(left_jewels)
            jewels.sort()

            score = sum(jewels)

            for i in range(min(K - A - B, len(jewels))):
                jewel = jewels[i]
                if jewel < 0:
                    score -= jewel
                else:
                    break
                    
            ret = max(ret, score)
                
    print(ret)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys


def main():
    # 読み込み回数が多い場合
    input = sys.stdin.readline
    # 複数
    N, K = map(int, input().split())

    # リスト読み込み
    V_A = [inp for inp in map(int, input().split())]
    V_B = V_A.copy()[::-1]
    
    ret = 0
    for A in range(K):
        if A < K:
            for B in range(K - A):
                left_jewels = V_A.copy()[:A]
                right_jewels = V_B.copy()[:B]

                if len(left_jewels) >= len(right_jewels):
                    jewels = left_jewels
                    jewels.extend(right_jewels)
                else:
                    jewels = right_jewels
                    jewels.extend(left_jewels)
                jewels.sort()

                score = sum(jewels)

                for i in range(min(K - A - B, len(jewels))):
                    jewel = jewels[i]
                    if jewel < 0:
                        score -= jewel
                    else:
                        break
                ret = max(ret, score)
        else:
            jewels = V_A.copy()[:A]
            score = sum(jewels)
            ret = max(ret, score)
                
    print(ret)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys


def main():
    # 読み込み回数が多い場合
    input = sys.stdin.readline
    # 複数
    N, K = map(int, input().split())

    # リスト読み込み
    V_A = [inp for inp in map(int, input().split())]
    V_B = V_A.copy()[::-1]
    
    ret = 0
    for A in range(K + 1):
        if A < K:
            for B in range(K - A + 1):
                left_jewels = V_A.copy()[:A]
                right_jewels = V_B.copy()[:B]

                if len(left_jewels) >= len(right_jewels):
                    jewels = left_jewels
                    jewels.extend(right_jewels)
                else:
                    jewels = right_jewels
                    jewels.extend(left_jewels)
                jewels.sort()

                score = sum(jewels)

                for i in range(min(K - A - B + 1, len(jewels))):
                    jewel = jewels[i]
                    if jewel < 0:
                        score -= jewel
                    else:
                        break
                ret = max(ret, score)
        else:
            jewels = V_A.copy()[:A]
            score = sum(jewels)
            ret = max(ret, score)
                
    print(ret)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys


def main():
    # 読み込み回数が多い場合
    input = sys.stdin.readline
    # 複数
    N, K = map(int, input().split())

    # リスト読み込み
    V_A = [inp for inp in map(int, input().split())]
    V_B = V_A.copy()[::-1]
    
    ret = 0
    for A in range(K + 1):
        if A < K:
            for B in range(K - A + 1):
                left_jewels = V_A.copy()[:A]
                right_jewels = V_B.copy()[:B]

                if len(left_jewels) >= len(right_jewels):
                    jewels = left_jewels
                    jewels.extend(right_jewels)
                else:
                    jewels = right_jewels
                    jewels.extend(left_jewels)
                jewels.sort()

                score = sum(jewels)

                for i in range(min(K - A - B, len(jewels))):
                    jewel = jewels[i]
                    if jewel < 0:
                        score -= jewel
                    else:
                        break
                ret = max(ret, score)
        else:
            jewels = V_A.copy()[:A]
            score = sum(jewels)
            ret = max(ret, score)
                
    print(ret)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from copy import copy


def main():
    # 読み込み回数が多い場合
    input = sys.stdin.readline
    # 複数
    N, K = map(int, input().split())

    # リスト読み込み
    V_A = [inp for inp in map(int, input().split())]
    V_B = V_A.copy()[::-1]
    
    ret = 0
    for A in range(K + 1):
        for B in range(K + 1 - A):
            left_jewels = copy(V_A[:A])
            right_jewels = copy(V_B[:B])
            
            if len(left_jewels) >= len(right_jewels):
                jewels = left_jewels
                jewels.extend(right_jewels)
            else:
                jewels = right_jewels
                jewels.extend(left_jewels)
            jewels.sort()
            
            score = sum(jewels)
            ret = max(ret, score)
            
            for i in range(min(K - A - B, len(jewels))):
                score -= jewels[i]
                ret = max(ret, score)
                
    print(ret)

    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    for left in range(K):
        for right in range(K):
            if left + right > K:
                continue
            tmp = []
            for i in range(min(N, left)):
                tmp.append(V[i])

            for j in range(max(left,N-right),N,1):
                tmp.append(V[j])

            nokori = K - left - right
            tmp.sort()
            tmp_sum = sum(tmp)
            for k in range(min(nokori, len(tmp))):
                if tmp[k] < 0:
                    tmp_sum -= tmp[k]

            ans = max(ans, tmp_sum)

    print(ans)





if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    for left in range(K):
        for right in range(K):
            if left + right > K:
                continue
            tmp = []
            for i in range(min(N, left)):
                tmp.append(V[i])

            for j in range(min(N,right)):
                tmp.append(V[N-1-j])

            nokori = K - left - right
            tmp.sort()
            tmp_sum = sum(tmp)
            for k in range(min(nokori, len(tmp))):
                if tmp[k] < 0:
                    tmp_sum -= tmp[k]

            ans = max(ans, tmp_sum)

    print(ans)





if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from bisect import bisect

n, k = map(int, input().split())
*v, = map(int, input().split())
candidate = []
for i in range(k+1):
    for j in range(k-i+1):
        if j == 0:
            candidate.append(v[:i])
        else:
            candidate.append(v[:i]+v[(-1)*j:])
ans = 0
for cand in candidate:
    cand.sort()
    idx = bisect(cand, 0)
    rem = k-len(cand)
    p = min(idx, rem)
    if ans < sum(cand[p:]):
        ans = sum(cand[p:])
print(ans)

Submission status: Wrong Answer

Solution code:
#%%
n, k = map(int, input().split())
v = list(map(int, input().split()))
vv = v.copy()[::-1]
ans = 0

for i in range(0, min(n+1, k+1)):
    for j in range(0, min(n+1, k+1, min(n+1, k+1) - i)):
        x = k - i - j
        tmp = v[:i] + vv[:j]
        tmp.sort()
        #print(tmp, x)
        t = 0
        for i in range(len(tmp)):
            if tmp[i] < 0 and (i+1) <= x:
                pass
            else:
                t += tmp[i]
        ans = max(ans, t)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split( ))
v = list(map(int, input().split( )))
import bisect
ans = 0
for i in range(k):
    for j in range(min(n-i,k-i)):
        u = v[:j]+v[n-i:]
        u.sort()
        ind = bisect.bisect_right(u,0)###right
        t = min(ind, k-i-j)
        sm = sum(u[t:])
        if ans <sm:
            #print(i,j,t)
            ans = sm
    
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split( ))
v = list(map(int, input().split( )))
import bisect
ans = 0
for i in range(k):
    for j in range(min(n-i,k-i)):
        u = v[:j]+v[n-i:]
        u.sort()
        ind = bisect.bisect_left(u,0)
        t = min(ind, k-i-j)
        sm = sum(u[t:])
        if ans <sm:
            #print(i,j,t)
            ans = sm
    
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
ans=0
for i in range(N):
    for j in range(i+1,N+1):
        if i+N-j<=K:
            k=K-(i+N-j)
            s=V[:i]+V[j:]
            s.sort()
            c=0
            for t in s:
                if k>=1 and t<0:
                    k-=1
                else:
                    c+=t
            ans=max(c,ans)
print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in range(K+1):
    left = []
    if i != 0:
        left = V[:i]
    for j in range(K-i+1):
        right = []
        if j == 0:
            right = []
        elif (N-i) == 0:
            right = []
        elif i+j > N:
            right = V[-(N-i):]
        else:
            right = V[-j:]
        for k in range(K-i-j+1):
            if k > i+j:
                continue
            sort_merge = sorted(left+right)
            ans = max(ans, sum(sort_merge[k:]))

print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in range(K+1):
    left = []
    if i != 0:
        left = V[:i]
    for j in range(K-i+1):
        right = []
        if j == 0:
            ans = max(ans, sum(left))
            continue
        elif (N-i) == 0:
            ans = max(ans, sum(left))
            continue
        elif i+j > N:
            right = V[-(N-i):]
        else:
            right = V[-j:]
        for k in range(K-i-j+1):
            if k > i+j:
                continue
            sort_merge = sorted(left+right)
            ans = max(ans, sum(sort_merge[k:]))

print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in range(K+1):
    left = []
    if i != 0:
        left = V[:i]
    for j in range(K-i+1):
        right = []
        if j == 0:
            continue
        elif (N-i) == 0:
            continue
        elif i+j > N:
            right = V[-(N-i):]
        else:
            right = V[-j:]
        for k in range(K-i-j+1):
            if k > i+j:
                continue
            sort_merge = sorted(left+right)
            ans = max(ans, sum(sort_merge[k:]))

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

ans = 0
for i in range(K+1):
    left = []
    if i != 0:
        left = sorted(V[:i])
    for j in range(K-i+1):
        right = []
        if j != 0:
            right = sorted(V[-j:])
        for k in range(K-i-j+1):
            if k > i+j:
                continue
            sort_merge = sorted(left+right)
            ans = max(ans, sum(sort_merge[k:]))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

import numpy as np

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

v = np.array(v)

ans = 0
for i in range(min(k+1, n+1)):
    for j in range(k-i+1):
        if j == 0:
            take = v[0:i]
        else:
            take = np.append(v[0:i], v[-j:])
        take.sort()
        throw_num = k-i-j
        throw_num = min(throw_num, len(take[take<0]))
        su = take[throw_num:].sum()
        if su > ans:
            ans = su
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

import numpy as np

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

v = np.array(v)
k = min(n, k)
ans = 0
for i in range(k+1):
    for j in range(k-i+1):
        if j == 0:
            take = v[0:i]
        else:
            take = np.append(v[0:i], v[-j:])
        take.sort()
        throw_num = k-i-j
        throw_num = min(throw_num, len(take[take<0]))
        su = take[throw_num:].sum()
        if su > ans:
            ans = su
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

import numpy as np

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

v = np.array(v)

ans = 0
for i in range(k+1):
    for j in range(k-i+1):
       
        if j == 0:
            take = v[0:i]
        else:
            take = np.append(v[0:i], v[-j:])
        take.sort()
        throw_num = k-i-j
        take_neg = take[take<0]

        throw_num = throw_num if throw_num < len(take_neg) else len(take_neg)
        su = -take_neg[:throw_num].sum() + take.sum()
        if su > ans:
            ans = su
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

import numpy as np

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

v = np.array(v)

ans = 0
for i in range(k):
    for j in range(k-i):
        
        if j == 0:
            take = v[0:i]
        else:
            take = np.append(v[0:i], v[-j:])
        take.sort()

        throw_num = k-i-j
        take_neg = take[take<0]

        throw_num = throw_num if throw_num < len(take_neg) else len(take_neg)
        su = -take_neg[:throw_num].sum() + take.sum()
        if su > ans:
            ans = su
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K = map(int, raw_input().split())
V = np.array(map(int, raw_input().split()))
V_double = np.concatenate((V,V))

ans = 0
for i in xrange(1, K):
    for j in xrange(N-i,N+1):
        for k in xrange(1, K):
            seq = (V_double[j:j+k])
            temp = np.sum(seq) - np.sum(np.sort(seq[seq<0])[:K-k])
            if temp > ans:
                ans = temp

print ans

Submission status: Wrong Answer

Solution code:
import heapq
N,K = map(int,input().split(' '))
Vn = list(map(int,input().split(' ')))
max_sum = 0
max_get = min(51,K+1)
for i1 in range(1,max_get):# 取る個数を決める
    for i2 in range(i1): #K の場所を動かす
        minus_queue = []
        forward_num = i1 - i2
        back_num = i2
        all_sum = 0
        #前方のlist
        forward_list = Vn[0:forward_num]
        back_list = Vn[N-back_num:N]
        for val in forward_list:
            all_sum += val
            if val < 0:
                minus_queue.append(val)
        #後方のlist
        for val in back_list:
            all_sum += val
            if val < 0:
                minus_queue.append(val)
        heapq.heapify(minus_queue)
        dequeue_num = K-i1
        for i3 in range(dequeue_num): #戻すKを決める
            if len(minus_queue) > 0:
                val = heapq.heappop(minus_queue) 
                all_sum -= val
            else:
                break
        max_sum = max(max_sum,all_sum)
print(max_sum)

Submission status: Wrong Answer

Solution code:
import heapq
N,K = map(int,input().split(' '))
Vn = list(map(int,input().split(' ')))
max_sum = 0
for i1 in range(1,K+1):# 取る個数を決める
    for i2 in range(i1): #K の場所を動かす
        minus_queue = []
        forward_num = i1 - i2
        back_num = i2
        all_sum = 0
        #前方のlist
        forward_list = Vn[0:forward_num]
        back_list = Vn[N-back_num:N]
        for val in forward_list:
            all_sum += val
            if val < 0:
                minus_queue.append(val)
        #後方のlist
        for val in back_list:
            all_sum += val
            if val < 0:
                minus_queue.append(val)
        heapq.heapify(minus_queue)
        for i3 in range(K-i1): #戻すKを決める
            if len(minus_queue) > 0:
                val = heapq.heappop(minus_queue) 
                all_sum -= val
            else:
                break
        max_sum = max(max_sum,all_sum)
print(max_sum)

Submission status: Wrong Answer

Solution code:
import heapq


def one_direction(a, j):
    res = 0
    for u in range(0, min(len(a)+1,j+1)):
        l = []
        heapq.heapify(l)
        sum = 0
        for w in range(u):
            sum += a[w]
            heapq.heappush(l, a[w])
        for y in range(k-u):
            if len(l) > 0:
                f = heapq.heappop(l)
                if f < 0:
                    sum -= f
                else:
                    break
        res = max(res, sum)
    return res




n,k = [int(_) for _ in input().split()]
v = [int(_) for _ in input().split()]
a = []
dp = [[0 for _ in range(k+1)] for _ in range(n+1)]
for i in range(1, n+1):
    x = v.pop()
    a.append(x)
    for j in range(1, k+1):
        ans1 = dp[i-1][j-1] + x
        ans2 = -float('inf')
        if j >= 2:
            ans2 = dp[i-1][j-2]
        ans3 = one_direction(a, j)
        dp[i][j] = max(ans1, ans2, ans3)
print(dp[n][k])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
V=[int(x) for x in input().split()]
max_value=0
for i in range(1,K+1):#左右から取れるだけ取る。
  for j in range(1,K+1):
    if i+j>=K or i+j>=N:
      break
    else:
      T=V[:i]+V[-j:]#マイナスは捨てたい。捨てる回数を考える。
      T.sort
      p=K-i-j#残りの作業回数
      value=0
      if p%2==0:
        for k in range(0,p+1,2):
          value=max(value,sum(T[k:]))
      else:
        for k in range(1,p+1,2):
          value=max(value,sum(T[k:]))
    max_value=max(max_value,value)
print(max_value)

Submission status: Wrong Answer

Solution code:
import itertools
N,K = map(int,input().split())
V=[int(x) for x in input().split()]
max_value=0
for i in range(1,K+1):#左右から取れるだけ取る。
  for j in range(1,K+1):
    if i+j>=K or i+j>=N:
      break
    else:
      T=V[:i]+V[-j:]
      T.sort()
      for k in range(len(T)):
        if T[k]<0:
          T[k]=0
    max_value=max(max_value,sum(T))
print(max_value)

Submission status: Wrong Answer

Solution code:
import itertools
N,K = map(int,input().split())
V=[int(x) for x in input().split()]
max_value=0
for i in range(1,K+1):#左右から取れるだけ取る。
  for j in range(1,K+1):
    if i+j>=K or i+j>=N:
      break
    else:
      T=V[:i]+V[-j:]
      T.sort()
      T=T[K-i-j:]#安いほうを捨てる
    max_value=max(max_value,sum(T))
print(max_value)

Submission status: Wrong Answer

Solution code:
import heapq
import copy

N, K = map(int, input().split())
V = list(map(int, input().split()))
max_sum = 0
for a in range(0, K+1):
    for b in range(0, K+1-a):
        sum_list=copy.copy(V[:a]+V[N-b:])
        heapq.heapify(sum_list)
        for i in range(K-a-b):
            print(i)
            if sum_list:
                q=heapq.heappop(sum_list)
                if q>0:
                    heapq.heappush(sum_list, q)
                    break
            else:
                break
        # print(a, b,K-a-b,V[:a]+V[N-b:], sum_list)
        max_sum = max(max_sum, sum(sum_list))
print(max_sum)

Submission status: Wrong Answer

Solution code:
import heapq
import copy

N, K = map(int, input().split())
V = list(map(int, input().split()))
max_sum = 0
for a in range(0, K+1):
    for b in range(0, K+1-a):
        sum_list=copy.copy(V[:a]+V[N-b:])
        heapq.heapify(sum_list)
        for i in range(K-a-b):
            if sum_list:
                q=heapq.heappop(sum_list)
                if q>0:
                    heapq.heappush(sum_list, q)
            else:
                break
        # print(a, b,K-a-b,V[:a]+V[K-b:], sum_list)
        max_sum = max(max_sum, sum(sum_list))
print(max_sum)

Submission status: Wrong Answer

Solution code:
#12:10
n,k = map(int,input().split())
v = list(map(int,input().split()))
can = []
for i in range(n):
  if i == 0:
    a = []
  elif i > k:
    break
  else:
    a.append(v[i-1])
  for j in range(n):
    if j == 0:
      b = []
    elif i + j > n or i + j > k:
      break
    else:
      b.append(v[-j])
    c = a + b
    c.sort()
    rest = min(k-i-j,len(c))
    tmp = sum(c)
    for de in range(rest):
      if c[de] < 0:
        tmp -= c[de]
      else:
        break
    can.append(tmp)
    #print(i,j,a,b,tmp)
print(max(can))

Submission status: Wrong Answer

Solution code:
#12:10
n,k = map(int,input().split())
v = list(map(int,input().split()))
can = []
for i in range(n):
  if i == 0:
    a = []
  elif i <= k:
    a.append(v[i-1])
  for j in range(n):
    if j == 0:
      b = []
    elif i + j <= n and i + j <= k:
      b.append(v[-j])
    c = a + b
    c.sort()
    rest = min(k-i-j,len(c))
    tmp = sum(c)
    for de in range(rest):
      if c[de] < 0:
        tmp -= c[de]
    can.append(tmp)
    #print(i,j,a,b,tmp)
print(max(can))

Submission status: Wrong Answer

Solution code:
import heapq

N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = float("inf") * (-1)
for d in range(K):
	p = K-d
	if not(d < p <= N):
		continue	
	for l in range(p+1):
		r = p-l
		q = []
		for i in range(l):
			heapq.heappush(q,V[i])
		for i in range(r):
			heapq.heappush(q,V[-(i+1)])
		for i in range(d):
			heapq.heappop(q)
		ans = max(ans, sum(q))

print(max(ans, 0))

Submission status: Wrong Answer

Solution code:
import copy
n,k=map(int,input().split())
l=list(map(int,input().split()))
lr=[[0,[]]]
ll=[[0,[]]]
sumr,suml=0,0
lrr=[]
llr=[]
for i in range(n):
  suml+=l[i]
  sumr+=l[-1-i]
  if l[i]<0:
    llr.append(l[i])
  if l[-1-i]<0:
    lrr.append(l[-1-i])
  llr1=copy.deepcopy(llr)
  lrr1=copy.deepcopy(lrr)
  lr.append([sumr,lrr1])
  ll.append([suml,llr1])
print(ll)
print(lr)
max=-10**10
for i in range(n+1):
  for j in range(n+1):#左からi個右からj個
    if i+j>n or i+j>k:
      pass
    else:
      max1=lr[j][0]+ll[i][0]
      num=k-i-j
      list1=lr[j][1]+ll[i][1]
      list1=sorted(list1)
      for o in range(min(len(list1),num)):
        max1-=list1[o]
        if max1>max:
          max=max1
      if max1>max:
        max=max1
print(max)

Submission status: Wrong Answer

Solution code:
import copy
n,k=map(int,input().split())
l=list(map(int,input().split()))
lr=[[0,[]]]
ll=[[0,[]]]
sumr,suml=0,0
lrr=[]
llr=[]
for i in range(n):
  suml+=l[i]
  sumr+=l[-1-i]
  if l[i]<0:
    llr.append(l[i])
  if l[-1-i]<0:
    lrr.append(l[-1-i])
  llr1=copy.deepcopy(llr)
  lrr1=copy.deepcopy(lrr)
  lr.append([sumr,lrr1])
  ll.append([suml,llr1])
max=-10**10
for i in range(n+1):
  for j in range(0,n+1):#左からi右からj
    if i+j>n or i+j>k:
      pass
    else:
      max1=lr[j][0]+ll[i][0]
      num=k-i-j
      list1=lr[j][1]+ll[i][1]
      list1=sorted(list1)
      for o in range(min(len(list1),num)):
        max1-=list1[o]
        if max1>max:
          max=max1
print(max)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
V=list(map(int,input().split()))

left=[0]
right=[0]

for n in range(N):
    left.append(V[n]+left[n])
for n in range(N):
    right.append(V[-n-1]+right[n])

#print(left)
#print(right)

dp=[[0]*(K+1) for _ in range(K+1)]

for l in range(min(K+1,N+1)):
    for r in range(min(K-l+1,N-l+1)):
        dp[l][r]=left[l]+right[r]
        if N>l+r:
            temp=V[:l]+V[::-1][:r]#ここが不味い
        else:
            temp=V[:]
        #print(temp)
        temp.sort()
        
        for i in range(min(K-l-r,N-l-r)):
            #ここでマイナス項を引きたい
            if len(temp)==0 or temp[0]>=0:
                break
            else:
                dp[l][r]-=temp.pop(0)
ans=0
for item in dp:
    for obj in item:
        ans=max(ans,obj)
#print(dp)
print(ans)