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:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 11 12:47:17 2019

@author: YudNK
"""

N, K = map(int,input().split())
D = [int(i) for i in input().split()]
M = min(N, K)
max = 0
for a in range(M + 1):
    l = (M+1)//2 - a
    if l < 0:
        l = 0
    for b in range(l,M - a + 1):
        C = []
        t = K - (a + b)
        C.extend(D[:a])
        C.extend(D[N - b:])
        C.sort()
        C = C[t:]
        if sum(C) > max:
            max = sum(C)
print(max)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
d = list(map(int, input().split()))

ans = 0
for i in range(1, min(k + 1, len(d))):
    s = 0
    for left in range(i+1):
        j = []
        if left > 0:
            j.extend(d[:left])
        if i-left > 0:
            j.extend(d[min(-(i - left), -1):])
        j = sorted(j)
        s = sum(j)
        for m in range(k - i):
            if len(j) <= m:
                break
            elif j[m] >= 0:
                break
            else:
                s -= j[m]
        ans = max(s, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3.4

# abc128_d

import collections
import itertools

def first_true(iterable, default=False, pred=None):
    return next(filter(pred, iterable), default)

def slice_deque(deq, t, s):
    return itertools.islice(deq, t, s)

def search_deque(deq, n):
    for l in range(n+1):
        for _ in range(l+1):
            yield list(slice_deque(deq, 0, l))
            deq.rotate(-1)
        deq.rotate(-l)
        deq.reverse()

def main():
    # Input
    n, k = [int(s) for s in input().split()]
    vs = (int(s) for s in input().split())
    # Get ans
    vs = collections.deque(vs)
    R = min(n, k)
    def gene_tv():
        for taken in search_deque(vs, R):
            #print(taken)
            l = len(taken)
            taken.sort()
            i = first_true(taken, 0, lambda x: x>0)
            r = min(i, k-l)
            yield sum(taken[r:])
    ans = max(gene_tv())
    # Output
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    V_list = list(map(int, input().split()))
    value_list = [0]
    for l in range(0, K+1):
        if l > N: break
        for r in range(0, K-l+1 ):
            if l+r > N: break
            have_list = V_list[:l]
            have_list += V_list[N-r:]
            have_list = sorted(have_list)
            if K-l-r == 0: continue
            for d in range(0, K-l-r):
                if have_list == []: continue
                if have_list[0] < 0: have_list.pop(0)
            value_list.append(sum(have_list))
    print(max(value_list))
    
main()

Submission status: Wrong Answer

Solution code:
import sys 

def create_K_list(K, K_list, k):
    new_list = []
    for k_list in K_list:
        if k_list[-1] == 0:
            new_list.append(k_list + [0])
            continue
        if (K//2)+1 >= k:
            for i in range(1,3):
                new_list.append(k_list + [i])
        else:
            for i in range(3):
                new_list.append(k_list + [i])
    return new_list

def run(K_list, V_list):
    have_list = []
    for K in K_list:
        if K == 1: have_list.append(V_list.pop(0))
        elif K == 2: have_list.append(V_list.pop(-1))
        elif K == 0: 
            have_list = sorted(have_list)
            have_list.pop(0)
    return sum(have_list)
          
def main():
    N, K = map(int, input().split())
    V_list = list(map(int, input().split()))
    K_list = [[1],[2]]
    for k in range(2, K+1):
        K_list = create_K_list(K, K_list, k)
    value_list = []
    for k in range(len(K_list)):
        V_list_cp = V_list[:]
        value_list.append(run(K_list[k], V_list_cp))
    max_value = max(value_list)
    if max_value <= 0: print(0)
    else: print(max_value)
        
main()

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(1000000)
 
N,K = map(int,input().split())
VN =  [int(i) for i in input().split()]

ans = 0
temp = 0

for i in range(min(N,K)):
    for j in range(min(K-i,N-i)):
        now = 0
        if i + j > min(N,K):
            break
       
        tmp_list = []
        
        for l in range(i):
            now += VN[l]
            tmp_list.append(VN[l])
    
        for m in range(j):
            now += VN[N-1-m]
            tmp_list.append(VN[N-1-m])
        
        tmp_list.sort()
        
        for n in range(K-i-j):
            if n >= len(tmp_list):
                break
            if tmp_list[n] >= 0:
                break
            now -= tmp_list[n]
    ans = max(ans,now)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(1000000)
 
N,K = map(int,input().split())
VN =  [int(i) for i in input().split()]

ans = 0
temp = 0

for i in range(min(N,K)):
    for j in range(min(K-i,N-i)):
        now = 0
        if i + j > min(N,K):
            break
       
        tmp_list = []
        
        for l in range(i):
            now += VN[l]
            tmp_list.append(VN[l])
    
        for m in range(j):
            now += VN[N-1-m]
            tmp_list.append(VN[N-1-m])
        
        tmp_list.sort()
        
        for n in range(K-i-j):
            if n >= len(tmp_list):
                break
            if tmp_list[n] >= 0:
                break
            now -= tmp_list[n]
        ans = max(ans,now)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(1000000)
 
N,K = map(int,input().split())
VN =  [int(i) for i in input().split()]

ans = 0
temp = 0

for i in range(min(N,K)):
    for j in range(min(K-i,N-i)):
        now = 0
        if i + j > min(N,K):
            break
       
        tmp_list = []
        
        for l in range(i):
            now += VN[l]
            tmp_list.append(VN[l])
    
        for m in range(j):
            now += VN[N-1-m]
            tmp_list.append(VN[N-1-m])
        
        tmp_list.sort()
        
        for n in range(K-i-j):
            if n >= len(tmp_list)-1:
                break
            if tmp_list[n] >= 0:
                break
            now -= tmp_list[n]
        ans = max(ans,now)

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):
	for j in range(k-i+1):
		# print(i,j)
		a = v[:i]
		b = v[-j:]
		if len(b) == n:
			b = []
		ab = sorted(a+b+[0])
		idx = min(ab.index(0),k-i-j)
		ans = max(ans,sum(ab[idx:]))
		# print(a,b,c,idx)
print(ans)

Submission status: Wrong Answer

Solution code:
class MyClass():
    maxNumber = 0

    def roop(self, baseList, opeCount):
        listCount = len(baseList)
        roopMax = listCount if listCount < opeCount else opeCount
        for leftCount in range(1, roopMax):
            for rightCount in range(0, roopMax - leftCount -1):
                leftList = baseList[0: leftCount]
                rightList = baseList[listCount - 1 - rightCount : listCount]
                handList = leftList + rightList

                self.getMax(handList, opeCount - leftCount - rightCount)

                

    def getMax(self, handList, remainOpeCount):
        if len(handList) < 1:
            return

        maxVal = 0
        removeCount = 0

        if remainOpeCount > 0:
            handList.sort()
            for each in handList:
                if each < 0 and remainOpeCount > 0:
                    removeCount += 1
                    remainOpeCount -= 1
        
        maxVal = sum(handList[removeCount:])
        self.maxNumber = max([maxVal, self.maxNumber])


count, opeCount = map(int, input().split())
baseList = list(map(int, input().split()))

my = MyClass()
my.roop(baseList, opeCount)
print(my.maxNumber)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
D = [int(i) for i in input().split()]
ans = 0

for i in range(min(n, -~k)):
  for j in range(-~i):
    having_list = D[:j] + D[n-i+j:]
    having_list.sort()
    having_list.append(0)
    l = 0
    while having_list[l] < 0 and k-i-l > 0:
      l += 1
    ans = max(ans, sum(having_list[l:]))
    

print(ans)

Submission status: Wrong Answer

Solution code:
class MyClass():
    maxNumber = 0

    def roop(self, baseList, opeCount):
        listCount = len(baseList)
        roopMax = listCount if listCount < opeCount else opeCount
        for leftCount in range(1, roopMax):
            for rightCount in range(0, roopMax - leftCount -1):
                leftList = baseList[0: leftCount - 1:]
                rightList = baseList[listCount - 1 - rightCount : listCount - 1]
                handList = leftList + rightList

                self.getMax(handList, opeCount - leftCount)

                

    def getMax(self, handList, remainOpeCount):
        if len(handList) < 1:
            return

        maxVal = 0
        removeCount = 0

        handList.sort()
        for each in handList:
            if each < 0 and remainOpeCount > 0:
                removeCount += 1
                remainOpeCount -= 1
        
        maxVal = max(handList[removeCount:])
        self.maxNumber = max([maxVal, self.maxNumber])


count, opeCount = map(int, input().split())
baseList = list(map(int, input().split()))

my = MyClass()
print(my.maxNumber)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(1000000)
 
N,K = map(int,input().split())
VN =  [int(i) for i in input().split()]

ans = 0
temp = 0

for i in range(min(N,K)+1):
    for j in range(min(K-i,N-i)):
        now = 0
        if i + j > min(N,K):
            break
       
        tmp_list = []
        
        for l in range(i):
            now += VN[l]
            tmp_list.append(VN[l])
    
        for m in range(j):
            now += VN[N-1-m]
            tmp_list.append(VN[N-1-m])
        
        tmp_list.sort()
        
        for n in range(K-i-j):
            if n >= len(tmp_list)-1:
                break
            if tmp_list[n] >= 0:
                break
            now -= tmp_list[n]
        ans = max(ans,now)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(1000000)
 
N,K = map(int,input().split())
VN =  [int(i) for i in input().split()]

ans = 0
temp = 0

for i in range(min(N,K)+1):
    for j in range(min(K-i,N-i)):
        now = 0
        if i + j > min(N,K):
            break
       
        tmp_list = []
        
        for l in range(i):
            now += VN[l]
            tmp_list.append(VN[l])
    
        for m in range(j):
            now += VN[N-1-m]
            tmp_list.append(VN[N-1-m])
        
        tmp_list.sort()
        
        for n in range(N-i-j-2):
            if n >= len(tmp_list):
                break
            if tmp_list[n] >= 0:
                break
            now -= tmp_list[n]
        ans = max(ans,now)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(1000000)
 
N,K = map(int,input().split())
VN =  [int(i) for i in input().split()]

ans = 0
temp = 0

for i in range(min(N,K)+1):
    for j in range(min(K-i,N-i)):
        now = 0
        if i + j > min(N,K):
            break
       
        tmp_list = []
        
        for l in range(i):
            now += VN[l]
            tmp_list.append(VN[l])
    
        for m in range(j):
            now += VN[N-1-m]
            tmp_list.append(VN[N-1-m])
        
        tmp_list.sort()
        
        for n in range(N-i-j-2):
            if n >= len(tmp_list)-1:
                break
            if tmp_list[n] >= 0:
                break
            now -= tmp_list[n]
        ans = max(ans,now)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(1000000)
 
N,K = map(int,input().split())
VN =  [int(i) for i in input().split()]

ans = 0
temp = 0

for i in range(min(N+1,K)):
    for j in range(min(K-i,N-i+1)):
        now = 0
        if i + j > K:
            break
       
        tmp_list = []
        
        for l in range(i):
            now += VN[l]
            tmp_list.append(VN[l])
    
        for m in range(j):
            now += VN[N-1-m]
            tmp_list.append(VN[N-1-m])
        
        tmp_list.sort()
        
        for n in range(N-i-j-2):
            if n >= len(tmp_list)-1:
                break
            if tmp_list[n] >= 0:
                break
            now -= tmp_list[n]
        ans = max(ans,now)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(1000000)
 
N,K = map(int,input().split())
VN =  [int(i) for i in input().split()]

ans = 0
temp = 0

for i in range(min(N,K)+1):
    for j in range(min(K-i,N-i)+1):
        now = 0
        if i + j > K:
            break
       
        tmp_list = []
        
        for l in range(i):
            now += VN[l]
            tmp_list.append(VN[l])
    
        for m in range(j):
            now += VN[N-1-m]
            tmp_list.append(VN[N-1-m])
        
        tmp_list.sort()
        
        for n in range(N-i-j-2):
            if n >= len(tmp_list)-1:
                break
            if tmp_list[n] >= 0:
                break
            now -= tmp_list[n]
        ans = max(ans,now)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(1000000)
 
N,K = map(int,input().split())
VN =  [int(i) for i in input().split()]

ans = 0
temp = 0

for i in range(min(N,K)):
    for j in range(min(K-i,N-i)):
        now = 0
        if i + j > K-2:
            break
       
        tmp_list = []
        
        for l in range(i):
            now += VN[l]
            tmp_list.append(VN[l])
    
        for m in range(j):
            now += VN[N-1-m]
            tmp_list.append(VN[N-1-m])
        
        tmp_list.sort()
        
        for n in range(N-i-j-2):
            if n >= len(tmp_list)-1:
                break
            if tmp_list[n] >= 0:
                break
            now -= tmp_list[n]
        ans = max(ans,now)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(1000000)
 
N,K = map(int,input().split())
VN =  [int(i) for i in input().split()]

ans = 0
temp = 0

for i in range(min(N,K)):
    for j in range(min(K-i,N-i)):
        now = 0
        if i + j > K:
            break
       
        tmp_list = []
        
        for l in range(i):
            now += VN[l]
            tmp_list.append(VN[l])
    
        for m in range(j):
            now += VN[N-1-m]
            tmp_list.append(VN[N-1-m])
        
        tmp_list.sort()
        
        for n in range(N-i-j-2):
            if n >= len(tmp_list)-1:
                break
            if tmp_list[n] >= 0:
                break
            now -= tmp_list[n]
        ans = max(ans,now)

print(ans)

Submission status: Wrong Answer

Solution code:
def A_method(A, V_):
    return V_[A:], V_[:A]
 
def B_method(B, V_):
    return V_[:len(V_)-B], V_[len(V_)-B:]
 
def CD_method(CD, H_):
    c = 0
    for h in H_:
        if h < 0:
            c += 1
    for _ in range(min(CD, c)):
        idx = min(enumerate(H_), key=lambda x: x[1])[0]
        H_[idx] = 0
    return H_
 
N, K = map(int, input().split())
V = list(map(int, input().split()))
 
M = min(N, K)
best_value = 0
 
for A in range(M+1):
    new_V, A_h = A_method(A, V)
    for B in range(M+1-A):
        new_V, B_h = B_method(B, new_V)
        H = CD_method(K-A-B, A_h+B_h)
        if sum(H) > best_value:
            best_value = sum(H)
print(best_value)

Submission status: Wrong Answer

Solution code:
def A_method(A, V_):
    return V_[A:], V_[:A]

def B_method(B, V_):
    return V_[:len(V_)-B], V_[len(V_)-B:]

def CD_method(CD, H_):
    c = 0
    for h in H_:
        if h < 0:
            c += 1
    for _ in range(min(CD, c)):
        idx = min(enumerate(H_), key=lambda x: x[1])[0]
        H_[idx] = 0
    return H_

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

M = min(N, K)
best_value = 0

for A in range(M+1):
    new_V, A_h = A_method(A, V)
    for B in range(M+1-A):
        new_V, B_h = B_method(B, new_V)
        H = CD_method(M-A-B, A_h+B_h)
        if sum(H) > best_value:
            best_value = sum(H)
print(best_value)

Submission status: Wrong Answer

Solution code:
import sys 

def create_K_list(K, K_list):
    new_list = []
    for k_list in K_list:
        if k_list[-1] == 0: continue
        for i in range(5):
            new_list.append(k_list + [i])
    return new_list

def run(K_list, V_list):
    have_list = []
    for K in K_list:
        try:
            if K == 1: have_list.append(V_list.pop(0))
            elif K == 2: have_list.append(V_list.pop(-1))
            elif K == 3: V_list.insert(0, sorted(have_list).pop(0))
            elif K == 4: V_list.insert(-1, sorted(have_list).pop(0))
            else: break
        except:
            return 0
    return sum(have_list)
          
def main():
    N, K = map(int, input().split())
    V_list = list(map(int, input().split()))
    K_list = [[0],[1],[2],[3],[4]]
    for k in range(K-1):
        K_list = create_K_list(K, K_list)
    
    
    value_list = []
    for k in range(len(K_list)):
        V_list_cp = V_list[:]
        value_list.append(run(K_list[k], V_list_cp))
    print(max(value_list))
        
main()

Submission status: Wrong Answer

Solution code:
import copy

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

max_value=0
for i in range(min(N,K)+1):
  for j in range(K-i+1):
    tmp_get=[]
    tmp_get.extend(V[:i])
    if j!=0:
      tmp_get.extend(V[-j:])
    tmp_get.sort()

    for _ in range(K-(i+j)):
      if tmp_get!=[] and tmp_get[0]<0:
        tmp_get.pop(0)

    if sum(tmp_get)>max_value:
      max_value=sum(tmp_get)
          
print(max_value)

Submission status: Wrong Answer

Solution code:

import copy

class MyClass():

    maxSum = 0

    def do(self, baseList, _temochi, leftIndex, rightIndex, nokoriSousaCount):
        if nokoriSousaCount < 0:
            return

        sum = 0
        for each in _temochi:
            sum += each

        if self.maxSum < sum:
            self.maxSum = sum

        if leftIndex <= rightIndex:
            temochi = copy.copy(_temochi)
            temochi.append(baseList[leftIndex])
            self.do(baseList, temochi, leftIndex + 1, rightIndex, nokoriSousaCount - 1)

            temochi = copy.copy(_temochi)
            temochi.append(baseList[rightIndex])
            self.do(baseList, temochi, leftIndex, rightIndex - 1, nokoriSousaCount - 1)
            
        #self.removeMin(_temochi, leftIndex, rightIndex, nokoriSousaCount - 1)

    def removeMin(self, _temochi, leftIndex, rightIndex, nokoriSousaCount):
        if len(_temochi) > 0 and nokoriSousaCount > 0:
            minNum = min(_temochi)
            if minNum < 0:
                temochi = copy.copy(_temochi)
                temochi.remove(minNum)
                self.do(baseList, temochi, leftIndex, rightIndex, nokoriSousaCount)


count, opeCount = map(int, input().split())
baseList = list(map(int, input().split()))

doClass = MyClass()
doClass.do(baseList, [], 0, count - 1, opeCount)

print(doClass.maxSum)

Submission status: Wrong Answer

Solution code:
import bisect

N, K = map(int, input().split())
V = [int(_) for _ in input().split()]
ans = 0
M = min(N, K)

for i in range(M+1):
    for j in range(M-i):
        tmplist = sorted(V[:i] + V[-1:-1-j:-1])
        minusindex = bisect.bisect_left(tmplist, 0)
        if minusindex == 0:
            tmpans = sum(tmplist)
        else:
            tmpans = sum(tmplist) - sum(tmplist[:min(minusindex, K-(i+j))])
            
        if tmpans > ans:
            ans = tmpans

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
V=[int(a) for a in input().split()]
able=min(N,K)
ans=0
 
for a in range(able+1):
    for b in range(able+1-a):
        if b==0:
            have=V[:a]
        else:
            have=V[:a]+V[-b:]
        have.sort()

        for i in range(min(a+b,able-a-b)+1):
            val=sum(have[i:])
            if ans<val:
                ans=val
 
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
V=[int(a) for a in input().split()]
able=min(N,K)
ans=0
 
for a in range(able+1):
    for b in range(able+1-a):
        if b==0:
            have=V[:a]
        else:
            have=V[:a]+V[-b:]
        have.sort()
        val=sum(have)

        if a+b==able:
            pass
        else:
            cnt=a+b
            for i in have:
                if i < 0:
                    val-=i
                    cnt+=1
                else:
                    break
                
                if cnt==able:
                    break
        
        if ans<val:
            ans=val
 
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
V=[int(a) for a in input().split()]
able=min(N,K)
ans=0
 
for a in range(able+1):
    for b in range(able+1-a):
        if b==0:
            have=V[:a]
        else:
            have=V[:a]+V[-b:]
        have.sort()
        val=sum(have)
        
        if a+b==able:
            pass
        else:
            for i in range(min(len(have),able+1-a-b)):
                if have[i] < 0:
                    val-=have[i]
                else:
                    break
        
        if ans<val:
            ans=val
 
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

num = 0
for line in sys.stdin:
    if int(num) == 0:
        N, K = map(int, line.strip().split(" "))
    else:
        V = list(map(int, line.strip().split(" ")))
    num += 1

#print(N, K, V)
price_list = []

V.extend(V)
# k件取得
for k in range(min(N,K)):
    for i in range(k+1):
        _V = V[N-i:N+k-i]
        _V.sort()
        #print(_V)
        price_list.append(sum(_V))

        # K-k件返す
        for j in range(K-k+1):
            if len(_V) == 0:
                continue
            _V.pop(0)
            #print(_V)
            price_list.append(sum(_V))

price_list.sort(reverse=True)
print(price_list[0])

Submission status: Wrong Answer

Solution code:
import sys

num = 0
for line in sys.stdin:
    if int(num) == 0:
        N, K = map(int, line.strip().split(" "))
    else:
        V = list(map(int, line.strip().split(" ")))
    num += 1

#print(N, K, V)
price_list = []

V.extend(V)
# k件取得
for k in range(min(N,K)):
    for i in range(k+1):
        _V = V[N-i:N+k-i]
        _V.sort()
        #print(_V)
        price_list.append(sum(_V))

        # K-k件返す
        for j in range(K-k):
            if len(_V) == 0:
                continue
            _V.pop(0)
            #print(_V)
            price_list.append(sum(_V))

price_list.sort(reverse=True)
print(price_list[0])

Submission status: Wrong Answer

Solution code:
#入力部
N, K = map(int, input().split())
V = list(map(int, input().split()))
V2 = list(reversed(V))  #反転したリスト
ans = 0 #最終出力


for i in range(K+1):
  for j in range(K+1-i):
    mylist = []
    #指定回数以上はbreak
    if i+j > K:
      break
    mylist = (V[:i])
    mylist.extend(V2[:j])
    #加算のみでの最大値を求める
    mysum = sum(mylist)
    #減算した方が良い場合を求める
    mylist.sort()
    for m in range(min(K - i - j, i+j)): #規定回数と実際に加算されている数を考慮
      #0以上の数は減算非対象
      if mylist[m] >= 0:
        break
      mysum -= mylist[m]
    #減算しない場合かする場合かの最大値が解
    ans = max(mysum, ans)
print(ans)

Submission status: Wrong Answer

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

R = min(N, K)
# 50 = min(100, 50)

ans = 0
# 左から取得する回数をA回, 右からをB回
# として、A + B <= R となるように全探索を行う

def filter_minus(x):
    if x < 0:
        return True
    else:
        return False


# 0, 1 ... 49, 50
for i in range(R+1):
    B = R - i
    # 50 - (0, 1 ... 49, 50) = 50, 49 ... 1, 0
    for j in range(B+1):
        left = V[:i]
        right = V[N-j:]

        k  = R - i - j

        
        values = left + right
        values.sort(reverse=True)
        
        for _ in range(k):
            if values and values[-1] < 0:
                values.pop() 
            
        total_v = sum(values)

        if total_v > ans:
            ans = total_v

print(ans)

Submission status: Wrong Answer

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

R = min(N, K)
# 50 = min(100, 50)

ans = 0
# 左から取得する回数をA回, 右からをB回
# として、A + B <= R となるように全探索を行う

def filter_minus(x):
    if x < 0:
        return True
    else:
        return False


# 0, 1 ... 49, 50
for i in range(R+1):
    B = R - i
    # 50 - (0, 1 ... 49, 50) = 50, 49 ... 1, 0
    for j in range(B+1):
        left = V[:i]
        right = V[N-j:]

        k  = R - i - j

        print(i, j, k)
        
        values = left + right
        values.sort(reverse=True)
        
        for _ in range(k):
            if values and values[-1] < 0:
                values.pop() 
            
        total_v = sum(values)

        if total_v > ans:
            ans = total_v

print(ans)

Submission status: Wrong Answer

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

R = min(N, K)
# 50 = min(100, 50)

ans = 0
# 左から取得する回数をA回, 右からをB回
# として、A + B <= R となるように全探索を行う

def filter_minus(x):
    if x < 0:
        return True
    else:
        return False


# 0, 1 ... 49, 50
for i in range(R+1):
    B = R - i
    # 50 - (0, 1 ... 49, 50) = 50, 49 ... 1, 0
    for j in range(B+1):
        left = V[:i]
        right = list(reversed(V))[:j]

        k  = R - i - j
        
        values = left + right
        values.sort(reverse=True)
        
        for l in range(k):
            if values and values[-1] < 0:
                values.pop() 
            
        total_v = sum(values)

        if total_v > ans:
            ans = total_v

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):
    for j in range(i+1):
        having = []
        having += V[:j]
        if j < i:
        	having += V[-(i-j):]
        having.sort()
        for k in range(K-i):
            if len(having) == 0: break
            if having[0] < 0:
                having.remove(having[0])
            else:
                break
        ans = max(sum(having), ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

N, K = map(int, raw_input().split())
V = map(int, raw_input().split())

score = -1e100

M = min(N, K)
for A in range(0, M + 1):
    for B in range(0, M - A + 1):
        S = []
        if A > 0:
            S += V[:A]
        if B > 0:
            S += V[-B:]
        S.sort()
        score = max(score, sum(S) - sum(filter(lambda x: x < 0, S)[:M - A - B]))

print score

Submission status: Wrong Answer

Solution code:
import bisect

N, K = map(int, raw_input().split())
V = map(int, raw_input().split())

score = -1e100

M = min(N, K)
for A in range(0, M + 1):
    for B in range(0, M - A + 1):
        S = V[:A] + (V[-B:] if B > 0 else [])
        S.sort()
        score = max(score, sum(S) - sum(filter(lambda x: x < 0, S)[:M - A - B]))

print score

Submission status: Wrong Answer

Solution code:
import bisect

N, K = map(int, raw_input().split())
V = map(int, raw_input().split())

score = -1e100

M = min(N, K)
for A in range(0, M + 1):
    for B in range(0, M - A + 1):
        S = V[:A] + (V[-B:] if B > 0 else [])
        S.sort()
        i = bisect.bisect_left(S, 1)
        score = max(score, sum(S[min(i, M - A - B):]))

print score

Submission status: Wrong Answer

Solution code:
import bisect

N, K = map(int, raw_input().split())
V = map(int, raw_input().split())

score = -1e100

M = min(N, K)
for A in range(0, M + 1):
    for B in range(0, M - A + 1):
        S = V[:A] + (V[-B:] if B > 0 else [])
        S.sort()
        i = bisect.bisect_left(S, 0)
        score = max(score, sum(S[min(i, M - A - B):]))

print score

Submission status: Wrong Answer

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

score = -1e100

M = min(N, K)
for A in range(0, M + 1):
    for B in range(0, M - A):
        S = V[:A] + (V[-B:] if B > 0 else [])
        S.sort()
        score = max(score, sum(S[M - A - B:]))

print score

Submission status: Wrong Answer

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

score = -1e100

M = min(N, K)
for A in range(0, M + 1):
    for B in range(0, M - A + 1):
        S = V[:A] + (V[-B:] if B > 0 else [])
        S.sort()
        score = max(score, sum(S[M - A - B:]))

print score

Submission status: Wrong Answer

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

Ntake=min(N,K)
Vsum=[]

for i in range(Ntake):
    M=K-i
    j=0
    while j<=i:
        Vtake=[]
        p=1
        while p<=j:
            Vtake.append(LV[p-1])
            p+=1
        p=1
        while p<=i-j:
            Vtake.append(LV[-p])
            p+=1
        Vtake.append(0)
        Vtake.sort()
        k=0
        while k<=M-1:
            if Vtake[0]<0:
                del Vtake[0]
                k+=1
            else:
                k+=M
        Vsum.append(sum(Vtake))
        j+=1

print(max(Vsum))