Problem: 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.

Submission attempts (in order):
import copy

N, K = map(int, input().split())
V = [int(i) for i in input().split()]

value = 0

for k in reversed(range(K)):
    for ret in range(K-k): 
        if (ret < k+1):
            for i in range(k+1+1): 
                tmp = []
                V_tmp = copy.copy(V)
                for sp in range(i):
                    tmp.extend(V_tmp[:i]) 
                    V_tmp = V_tmp[i:]
                    tmp.extend(V_tmp[-(k-i):]) 
                    V_tmp = V_tmp[:-(k-i)]
                    if V_tmp == []:
                        break
                print(tmp)
                tmp = list(filter(lambda x: x not in sorted(tmp)[:ret],tmp))
                value = max(value,sum(tmp))

print(value)

import copy

N, K = map(int, input().split())
V = [int(i) for i in input().split()]

value = 0

for k in reversed(range(K)):
    for ret in range(K-k): 
        if (ret < k+1):
            print('{}個取って{}戻す'.format(k+1,ret))
            for i in range(k+1+1): 
                tmp = []
                V_tmp = copy.copy(V)
                
                tmp.extend(V_tmp[:i]) 
                
                V_tmp = V_tmp[i:]
                
                if (k+1-i) > 0:
                    tmp.extend(V_tmp[-(k+1-i):]) 
                    
                # V_tmp = V_tmp[:-(k+1-i)]
                
                if V_tmp == []:
                    break
                tmp = list(filter(lambda x: x not in sorted(tmp)[:ret],tmp))
                print('手元の宝石：',tmp)
                value = max(value,sum(tmp))
            #print()

print(value)

import copy

N, K = map(int, input().split())
V = [int(i) for i in input().split()]

value = 0

for i in reversed(range(min(N,K)+1)):
    #print('i:',i)
    for sp in range(i+1): 
        #print('right',sp)
        #print('left',i-sp)
        tmp = []
        V_tmp = copy.copy(V)
        
        tmp.extend(V_tmp[:sp]) 
        #print('left',V_tmp[:sp])
        tmp.extend(V_tmp[N-(i-sp):]) 
        #print('right',V_tmp[N-(i-sp):])
        if V_tmp == []:
            break
        
        for ret in range(min(K-i,i)): 
            if (ret < i):
                #print('ret',ret)
                tmp = list(filter(lambda x: x not in sorted(tmp)[:ret],tmp))
                
                value = max(value,sum(tmp))
            #print()

print(value)

import copy

N, K = map(int, input().split())
V = [int(i) for i in input().split()]

result = 0

for i in reversed(range(min(N,K)+1)):
    #print('i:',i)
    for sp in range(i+1): 
        #print('right',sp)
        #print('left',i-sp)
        tmp = []
        V_tmp = copy.copy(V)
        
        tmp.extend(V_tmp[:sp]) 
        #print('left',V_tmp[:sp])
        tmp.extend(V_tmp[N-(i-sp):]) 
        #print('right',V_tmp[N-(i-sp):])
        value = sum(tmp)
        
        for ret in range(min(K-i,i)): 
            if ret < i:
                #print('ret',ret)
                if tmp[ret] < 0:
                    value -= tmp[ret]
                
                result = max(result,value)
            #print()

print(value)

import copy

N, K = map(int, input().split())
V = [int(i) for i in input().split()]

value = 0

for k in reversed(range(K)):
    for ret in range(K-k): 
        if (ret < k+1):
            for i in range(k+1+1): 
                tmp = []
                V_tmp = copy.copy(V)
                tmp.extend(V_tmp[:i]) 
                V_tmp = V_tmp[i:]
                tmp.extend(V_tmp[-(k+1-i):]) 
                V_tmp = V_tmp[:-(k+1-i)]
                if V_tmp == []:
                    break
                tmp = list(filter(lambda x: x not in sorted(tmp)[:ret],tmp))
                print(tmp)
                value = max(value,sum(tmp))

print(value)

import copy

N, K = map(int, input().split())
V = [int(i) for i in input().split()]

result = 0

for i in range(min(N,K)+1):
    #print('i:',i)
    for sp in range(i+1): 
        #print('right',sp)
        #print('left',i-sp)
        tmp = []
        V_tmp = copy.copy(V)
        
        tmp.extend(V_tmp[:sp]) 
        #print('left',V_tmp[:sp])
        tmp.extend(V_tmp[N-(i-sp):]) 
        #print('right',V_tmp[N-(i-sp):])
        #print(tmp)
        tmp = sorted(tmp)
        value = sum(tmp)
        
        for ret in range(0, min(K-i,i)): 
            if ret < i:
                
                if tmp[ret] < 0:
                    value -= tmp[ret]
                    
            else:
                break
        result = max(result,value)
            #print()

print(result)


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted