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:
N, K = map(int, input().split())
V = list(map(int, input().split()))

def dispose(disposeList, tms):
    disposeList.sort()
    i = 0
    while i < tms:
        if disposeList[i] < 0:
            disposeList[i] = 0
        else:
            return disposeList
        
    return disposeList
    
res = 0
for take in range(min(N,K)+1):
    for left in range(take+1):
        right = take - left
        my_V = [V[i] for i in range(left)] + [V[-i-1] for i in range(right)]
    
        my_V = dispose(my_V, min(take, K-take))
        res = max(res, sum(my_V))
        
print(res)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[30]:


import sys
#import numpy as np
#import itertools
input = sys.stdin.readline
N, K = map(int, input().split())
V = list(map(int, input().split()))


# In[36]:


#print(N, K)
#print(V)


# In[ ]:


"""
6 4
-10 8 2 1 2 6
"""


# In[45]:


ans = 0
for l in range(K+1):
    for r in range(K+1-l):
        if l+r>K:
            continue
        d = K-(l+r)
        picked = V[:l] + V[-r:] if r > 0 else V[:l]
        #print("l:{}, r:{}, picked{}".format(l, r, picked))
        sp = sorted(picked)
        now = sum(picked)
        #print(sp)
        for i in range(d):
            if i >= len(picked):
                break
            if sp[i] > 0:
                break
            now -= sp[i]
        ans = max(ans, now)
print(ans)
        
"""
if N < K:
    scs = []
    for i in range(K-N):
        scs.append(sorted(V[i:]))
    s = max(scs)
else:
    scs = []
    for i in range(K//2):
        #print("{}個返す".format(i))
        scss = []
        for j in range(K+1-i):
            #print("左から{}個　右から{}個/{}".format(j,K-j-i, K-i))
            if j == K-i:
                choices = V[:j]
            else:
                choices = V[:j] + V[-K+j+i:]
            #print(choices)
            #print(sorted(choices)[i:])
            scss.append(sum(sorted(choices)[i:]))
        scs.append(max(scss))
    s = max(scs)
    s = 0 if s<0 else s
print(s)
"""


# In[ ]:

Submission status: Wrong Answer

Solution code:
[input_n, input_k] = map(int, input().split())

input_v = list(map(int, input().split()))

max_value = 0

for a in range(min(input_n, input_k + 1)):
    for b in range(min(input_n, input_k - a + 1)):
        if (b != 0):
            hold_jewelries = input_v[:a] + input_v[-b:]
        else:
            hold_jewelries = input_v[:a]
        hold_jewelries = sorted(hold_jewelries)

        remove_length = input_k - a - b 
        hold_minus = sum(1 for value in hold_jewelries if value < 0)

        hold_value = sum(hold_jewelries[min(hold_minus, remove_length):])

        if (hold_value > max_value):
            max_value = hold_value

print(max_value)

Submission status: Wrong Answer

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

for i in range(2**min(N, K)):
  for j in range(min(N, K)):
      D = D_mem
      H = []
      for j1 in range(j):
        if (i>>j1) == 0:
          H.append(D[0])
          D = D[1:]
        else:
          H.append(D[-1])
          D = D[:-1]
      H.sort()
      for _ in range(min((K-j), len(H))):
        if H[0] < 0:
          H = H[1:]
        else:
          break
      val = sum(H)
  if ans < val:
      ans = val

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque

def solve():
  n,k = (int(i) for i in input().split())
  v = list(int(i) for i in input().split())
  ans = 0
  
  for atimes in range(k+1):
    for btimes in range(k-atimes+1):
      if [atimes,btimes] == [0,0]:continue
      
      getjewel = v[0:atimes]+v[n-btimes::]
      getjewel.sort()
      deletevalue = 0
      maxdeletetimes = k-atimes-btimes
      for i in range(min(len(getjewel),maxdeletetimes)):
        if getjewel[i] <= 0:
          deletevalue += getjewel[i]
      ans = max(sum(getjewel)-deletevalue,ans)
  print(ans)
solve()

Submission status: Wrong Answer

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

m = k // 2 + (k % 2)

a = [0] * (n+1)
d = [0] * (n+1)

a[0] = 0
d[0] = 0
for i in range(n):
    a[i+1] = a[i] + v[i]
    d[i+1] = d[i] + v[n-1-i]

w = []
for i in range(n):
    w.append([v[i], i])

w.sort()

ans = 0

p = min(n, k)
for L in range(p + 1):
    for R in range(p + 1 - L):
        #print(L, R)
        temp = a[L] + d[R]
        b = min(L + R, k - L - R)
        c = 0
        #print(temp)
        for _ in range(b):
            if w[c][0] >= 0:
                break
            if w[c][1] < L or w[c][1] > n - 1 - R:
                #print(w[c][1], L, R)
                temp -= w[c][0]
            c += 1
        #print(L, R, temp, b)
        ans = max(ans, temp)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
V = list(map(int,input().split()))
rV = V[::-1]
Q = []
m = min(n,k)
res = 0
for i in range(m+1):
    for j in range(m-i+1):
        Q.extend(V[:i])
        Q.extend(rV[:j])
        if not Q:
            continue
        s = sum(Q)
        Q.sort()
        for k in range(min(len(Q),k-len(Q))):
            if Q[k] < 0:
                s -= Q[k]   
        res = max(res,s)
        Q = []
print(res)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
V = list(map(int,input().split()))
rV = V[::-1]
Q = []
m = min(n,k)
res = 0
for i in range(m+1):
    for j in range(m-i+1):
        Q.extend(V[:i])
        Q.extend(rV[:j])
        if not Q:
            continue
        s = sum(Q)
        Q.sort()
        for k in range(min(i+j,m-i-j+1)):
            if Q[k] < 0:
                s -= Q[k]   
        res = max(res,s)
        Q = []
print(res)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
V = list(map(int,input().split()))
rV = V[::-1]
Q = []
m = min(n,k)
res = 0
for i in range(m+1):
    for j in range(m-i+1):
        Q.extend(V[:i])
        Q.extend(rV[:j])
        if not Q:
            continue
        s = sum(Q)
        Q.sort()
        for k in range(min(i+j,m-i-j)):
            if Q[k] < 0:
                s -= Q[k]   
        res = max(res,s)
        Q = []
print(res)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
V = list(map(int,input().split()))
rV = V[::-1]
Q = []
m = min(n,k)
res = 0
for i in range(m):
    for j in range(m-i):
        Q.extend(V[:i])
        Q.extend(rV[:j])
        if not Q:
            continue
        s = sum(Q)
        Q.sort()
        for k in range(min(i+j,m-i-j)):
            if Q[k] < 0:
                s -= Q[k]   
        res = max(res,s)
        Q = []
print(res)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
V = list(map(int,input().split()))
rV = V[::-1]
Q = []
m = min(n,k)
res = 0
for i in range(m):
    for j in range(m-i):
        Q.extend(V[:i])
        Q.extend(rV[:j])
        if not Q:
            continue
        s = sum(Q)
        Q.sort()
        for k in range(min(len(Q),m-i-j)):
            if Q[k] < 0:
                s -= Q[k]   
        res = max(res,s)
        Q = []
print(res)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
V = list(map(int,input().split()))
rV = V[::-1]
from heapq import heapify,heappop,heappush
Q = []
m = min(n,k)
res = 0
for i in range(m):
    for j in range(m-i+1):
        Q.extend(V[:i])
        Q.extend(rV[:j])
        cnt = i+j
        if not Q:
            continue
        s = sum(Q)
        Q.sort()
        for k in range(min(len(Q),m-i-j)):
            if Q[k] < 0:
                s -= Q[k]   
        res = max(res,s)
        Q = []
print(res)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
N,K=map(int,raw_input().split())
V=map(int,raw_input().split())
VR=list(reversed(V))
ans=-10**8
for l in range(K+1):
    for r in range(K+1):
        if l+r>K:continue
        d=K-l-r
        buf=V[:l]; buf+=VR[:r]
        buf.sort()
        buf.reverse()
        for i in range(d):
            if len(buf)>0 and buf[-1]<0:buf.pop()
        ans=max(ans,sum(buf))
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque, _heapq
N, K = map(int, input().split())
V = deque(map(int, input().split()))

class Money:
    def __init__(self):
        self.yen = 0
        self.red = []

    def value(self):
        return self.yen + sum(self.red)

    def add(self, yen):
        if yen >= 0:
            self.yen += yen
        else:
            self.red.append(yen)

    def remove(self, yen):
        if yen >= 0:
            self.yen -= yen
        else:
            self.red.remove(yen)

#mny = Money()
def get(cnt, mny, lstop, rstop):
    ans = mny.value()

    if cnt <= 0:
        return ans

    if len(V) > 0:

        if lstop == 0:
            w = V.popleft()
            mny.add(w)
            ans = max(ans, get(cnt-1, mny, lstop, rstop))
            mny.remove(w)
            V.appendleft(w)
        elif lstop > 0:
            lstop -= 1

        if rstop == 0:
            w = V.pop()
            mny.add(w)
            ans = max(ans, get(cnt-1, mny, -1, rstop))
            mny.remove(w)
            V.append(w)

    if len(mny.red) >= cnt // 2 + 1:
        w = min(mny.red)
        mny.red.remove(w)

        V.appendleft(w)
        ans = max(ans, get(cnt-1, mny, -1, rstop))
        V.popleft()

        V.append(w)
        ans = max(ans, get(cnt-1, mny, lstop, -1))
        V.pop()

        mny.red.append(w)    

    return ans

print(get(K, Money(), 0, 0))

Submission status: Wrong Answer

Solution code:
from collections import deque, _heapq
N, K = map(int, input().split())
V = deque(map(int, input().split()))

class Money:
    def __init__(self):
        self.yen = 0
        self.red = []

    def value(self):
        return self.yen + sum(self.red)

    def add(self, yen):
        if yen >= 0:
            self.yen += yen
        else:
            self.red.append(yen)

    def remove(self, yen):
        if yen >= 0:
            self.yen -= yen
        else:
            self.red.remove(yen)

#mny = Money()
def get(cnt, mny, lstop, rstop):
    ans = mny.value()

    if cnt <= 0:
        return ans

    if len(V) > 0:

        if lstop == 0:
            w = V.popleft()
            mny.add(w)
            ans = max(ans, get(cnt-1, mny, lstop, rstop))
            mny.remove(w)
            V.appendleft(w)
        elif lstop > 0:
            lstop -= 1

        if rstop == 0:
            w = V.pop()
            mny.add(w)
            ans = max(ans, get(cnt-1, mny, -1, rstop))
            mny.remove(w)
            V.append(w)

    if len(mny.red) >= cnt:
        w = min(mny.red)
        mny.red.remove(w)

        V.appendleft(w)
        ans = max(ans, get(cnt-1, mny, -1, rstop))
        V.popleft()

        V.append(w)
        ans = max(ans, get(cnt-1, mny, lstop, -1))
        V.pop()

        mny.red.append(w)    

    return ans

print(get(K, Money(), 0, 0))

Submission status: Wrong Answer

Solution code:
from collections import deque, _heapq
N, K = map(int, input().split())
V = deque(map(int, input().split()))

class Money:
    def __init__(self):
        self.yen = 0
        self.red = []

    def value(self):
        return self.yen + sum(self.red)

    def add(self, yen):
        if yen >= 0:
            self.yen += yen
        else:
            self.red.append(yen)

    def remove(self, yen):
        if yen >= 0:
            self.yen -= yen
        else:
            self.red.remove(yen)

#mny = Money()
def get(cnt, mny, lstop, rstop):
    ans = mny.value()

    if cnt <= 0:
        return ans

    if len(V) > 0:

        if lstop == 0:
            w = V.popleft()
            mny.add(w)
            ans = max(ans, get(cnt-1, mny, lstop, rstop))
            mny.remove(w)
            V.appendleft(w)
        elif lstop > 0:
            lstop -= 1

        if rstop == 0:
            w = V.pop()
            mny.add(w)
            ans = max(ans, get(cnt-1, mny, -1, rstop))
            mny.remove(w)
            V.append(w)

    if len(mny.red) >= cnt+1:
        w = min(mny.red)
        mny.red.remove(w)

        V.appendleft(w)
        ans = max(ans, get(cnt-1, mny, -1, rstop))
        V.popleft()

        V.append(w)
        ans = max(ans, get(cnt-1, mny, lstop, -1))
        V.pop()

        mny.red.append(w)    

    return ans

print(get(K, Money(), 0, 0))

Submission status: Wrong Answer

Solution code:
import sys
READi = lambda : map(int, sys.stdin.readline().strip().split())

N, K = READi()
X = min(N, K)
V = READi()

pat = 0

for i in xrange(X+1):
  i1,i2 = i, X-i
  l, r = V[:i1], V[::-1][:i2]

  for li in xrange(i1+1):
    for ri in xrange(i2+1):
      newl = l[:i1-li]
      newr = r[:i2-ri]

      if len(newl) + len(newr) < li+ri:
        continue

      for j in xrange(li+ri, -1, -2):
        ans = sum(sorted(newl+newr)[j:])
#        print l, r, "\t", newl, newr, "\t\t", sorted(newl+newr)[j:], "\t", ans

        if pat < ans:
          pat = ans

print pat

Submission status: Wrong Answer

Solution code:
import sys
READi = lambda : map(int, sys.stdin.readline().strip().split())

N, K = READi()
X = min(N, K)
V = READi()

pat = 0

for i in xrange(X+1):
  i1,i2 = i, X-i
  l, r = V[:i1], V[::-1][:i2]

  for li in xrange(i1+1):
    for ri in xrange(i2+1):
      newl = l[:i1-li]
      newr = r[:i2-ri]

      if len(newl) + len(newr) < li+ri:
        continue

      for j in xrange(li+ri, -1, -2):
        ans = sum(sorted(newl+newr)[j:])
        print l, r, "\t", newl, newr, "\t\t", sorted(newl+newr)[j:], "\t", ans

        if pat < ans:
          pat = ans

print pat

Submission status: Wrong Answer

Solution code:
import sys
READi = lambda : map(int, sys.stdin.readline().strip().split())

N, K = READi()
X = min(N, K)
V = READi()

pat = 0

for i in xrange(X+1):
  i1,i2 = i, X-i
  l, r = V[:i1], V[::-1][:i2]

  for li in xrange(i1+1):
    for ri in xrange(i2+1):
      newl = l[:i1-li]
      newr = r[:i2-ri]

      if len(newl) + len(newr) < li+ri:
        continue

      ans = sum(sorted(newl+newr)[li+ri:])
      print l, r, "\t", newl, newr, "\t\t", sorted(newl+newr)[li+ri:], "\t", ans

      if pat < ans:
        pat = ans

print pat

Submission status: Wrong Answer

Solution code:
import sys
READi = lambda : map(int, sys.stdin.readline().strip().split())

N, K = READi()
V = READi()

pat = 0

for i in xrange(K+1):
  KK = K
  while N < i or N < KK-i:
    KK -= 2
  l, r = V[:i], V[::-1][:KK-i]

  for li in xrange(i+1):
    newl = l[:i-li]
    for ri in xrange(KK-i+1):
      newr = r[:KK-i-ri]

      if len(newl) + len(newr) < li+ri: continue

      ans = sum(sorted(newl+newr)[li+ri:])
      #print l, r, "\t", newl, newr, "\t", sorted(newl+newr)[li+ri:], "\t", ans

      if pat < ans:
        pat = ans

print pat

Submission status: Wrong Answer

Solution code:
import sys
READi = lambda : map(int, sys.stdin.readline().strip().split())

N, K = READi()
V = READi()

pat = 0

for i in xrange(K+1):
  if N < i or N < K-i:
    continue
  l, r = V[:i], V[::-1][:K-i]

  for li in xrange(i+1):
    newl = l[:i-li]
    for ri in xrange(K-i+1):
      newr = r[:K-i-ri]

      if len(newl) + len(newr) < li+ri: continue

      ans = sum(sorted(newl+newr)[li+ri:])
      #print l, r, "\t", newl, newr, "\t", sorted(newl+newr)[li+ri:], "\t", ans

      if pat < ans:
        pat = ans

print pat

Submission status: Wrong Answer

Solution code:
import sys
READi = lambda : map(int, sys.stdin.readline().strip().split())

N, K = READi()
V = READi()

pat = 0

for i in xrange(K+1):
  l, r = V[:i], V[::-1][:K-i]

  for li in xrange(i+1):
    newl = l[:i-li]
    for ri in xrange(K-i+1):
      newr = r[:K-i-ri]

      if len(newl) + len(newr) < li+ri: continue

      ans = sum(sorted(newl+newr)[li+ri:])
      #print l, r, "\t", newl, newr, "\t", sorted(newl+newr)[li+ri:], "\t", ans

      if pat < ans:
        pat = ans

print pat

Submission status: Wrong Answer

Solution code:
import sys
READi = lambda : map(int, sys.stdin.readline().strip().split())

N, K = READi()
V = READi()

pat = 0

for i in xrange(K+1):
  l, r = [V[:i], V[::-1][:K-i]]

  for li in xrange(len(l)+1):
    newl = l[:len(l)-li]
    for ri in xrange(len(r)+1):
      newr = r[:len(r)-ri]

      ret = li + ri
      if len(newl) + len(newr) < ret: continue

      ans = sum(sorted(newl+newr)[li+ri:])

      if pat < ans:
        pat = ans

print pat

Submission status: Wrong Answer

Solution code:
import sys
READi = lambda : map(int, sys.stdin.readline().strip().split())

N, K = READi()
V = READi()

pat = 0

for i in xrange(K+1):
  l, r = [V[:i], V[::-1][:K-i]]
  ans = sum(l) + sum(r)
  if pat < ans:
    pat = ans

  for li in xrange(len(l)+1):
    newl = l[:len(l)-li]
    for ri in xrange(len(r)+1):
      newr = r[:len(r)-ri]
      ans = sum(sorted(newl)[li:]) + sum(sorted(newr)[ri:])
      if pat < ans:
        pat = ans

print pat

Submission status: Wrong Answer

Solution code:
from collections import deque, _heapq
N, K = map(int, input().split())
V = deque(map(int, input().split()))

class Money:
    def __init__(self):
        self.yen = 0
        self.red = []

    def value(self):
        return self.yen + sum(self.red)

    def add(self, yen):
        if yen >= 0:
            self.yen += yen
        else:
            self.red.append(yen)

    def remove(self, yen):
        if yen >= 0:
            self.yen -= yen
        else:
            self.red.remove(yen)

mny = Money()
def get(cnt, mny, lstop, rstop):
    ans = mny.value()

    if cnt <= 0:
        return ans

    if len(V) > 0:

        if lstop == 0:
            w = V.popleft()
            mny.add(w)
            ans = max(ans, get(cnt-1, mny, lstop, rstop))
            mny.remove(w)
            V.appendleft(w)
        elif lstop > 0:
            lstop -= 1

        if rstop == 0:
            w = V.pop()
            mny.add(w)
            ans = max(ans, get(cnt-1, mny, -1, rstop))
            mny.remove(w)
            V.append(w)

    if len(mny.red) >= cnt:
        w = min(mny.red)
        mny.red.remove(w)

        V.appendleft(w)
        ans = max(ans, get(cnt-1, mny, -1, rstop))
        V.popleft()

        V.append(w)
        ans = max(ans, get(cnt-1, mny, lstop, -1))
        V.pop()

        mny.red.append(w)    

    return ans

print(get(K, Money(), 0, 0))

Submission status: Wrong Answer

Solution code:
from collections import deque, _heapq
N, K = map(int, input().split())
V = deque(map(int, input().split()))

class Money:
    def __init__(self):
        self.yen = 0
        self.red = []

    def value(self):
        return self.yen + sum(self.red)

    def add(self, yen):
        if yen >= 0:
            self.yen += yen
        else:
            self.red.append(yen)

    def remove(self, yen):
        if yen >= 0:
            self.yen -= yen
        else:
            self.red.remove(yen)

mny = Money()
def get(cnt, mny, lstop, rstop):
    ans = mny.value()

    if cnt <= 0:
        return ans

    if len(V) > 0:

        if lstop == 0:
            w = V.popleft()
            mny.add(w)
            ans = max(ans, get(cnt-1, mny, lstop, rstop))
            mny.remove(w)
            V.appendleft(w)
        elif lstop > 0:
            lstop -= 1

        if rstop == 0:
            w = V.pop()
            mny.add(w)
            ans = max(ans, get(cnt-1, mny, 1, rstop))
            mny.remove(w)
            V.append(w)

    if len(mny.red) >= cnt:
        w = min(mny.red)
        mny.red.remove(w)

        V.appendleft(w)
        ans = max(ans, get(cnt-1, mny, -1, rstop))
        V.popleft()

        V.append(w)
        ans = max(ans, get(cnt-1, mny, lstop, -1))
        V.pop()

        mny.red.append(w)    

    return ans

print(get(K, Money(), 0, 0))

Submission status: Wrong Answer

Solution code:
import copy
import heapq
from collections import deque
N, K = list(map(int, input().split()))
V = list(map(int, input().split()))

ans = 0

for i in range(min(N, K+1)):
    for j in range(min(N-i, K+1-i)):
        s = V[:i] + V[N-j:]
        s.sort()
        m = 0
        for x in range(min(K - i - j, len(s))):
            if s[x] < 0:
                m += s[x]
            else:
                break

        ans = max(ans, sum(s) - m)

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np
import copy

def main():
	N, K = map(int,input().split())

	Vs = list(map(int,input().split()))

	allCandidates = []
	for i in range(int(K / 2) + 1):
		for one in range(0, K - i + 1):
			allCandidates.extend([[0] * one + [1] * (K - i - one) + [-1] * i])

	maxScore = 0
	for candidate in allCandidates:
		have = []
		vs = copy.deepcopy(Vs)
		exceptFlag = False
		for k in candidate:
			if k == 0:
				try:
					have.append(vs[0])
					del vs[0]
				except:
					exceptFlag = True
					break
			if k == 1:
				try:
					have.append(vs[-1])
					del vs[-1]
				except:
					exceptFlag = True
					break
			if k == -1:
				try:
					if have[np.argmin(have)] < 0:
						del have[np.argmin(have)]
				except:
					exceptFlag = True
					break
		if not exceptFlag:
			score = sum(have)
			if score > maxScore:
				maxScore = score

	print(maxScore)

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np
import copy

def main():
	N, K = map(int,input().split())

	Vs = list(map(int,input().split()))

	allCandidates = []
	for i in range(int(K / 2) + 1):
		for one in range(0, K - i + 1):
			allCandidates.extend([[0] * one + [1] * (K - i - one) + [-1] * i])

	maxScore = 0
	for candidate in allCandidates:
		have = []
		vs = copy.deepcopy(Vs)
		exceptFlag = False
		for k in candidate:
			if k == 0:
				try:
					have.append(vs[0])
					del vs[0]
					zeroCount += 1
				except:
					exceptFlag = True
					break
			if k == 1:
				try:
					have.append(vs[-1])
					del vs[-1]
				except:
					exceptFlag = True
					break
			if k == -1:
				try:
					if have[np.argmin(have)] < 0:
						del have[np.argmin(have)]
				except:
					exceptFlag = True
					break
		if not exceptFlag:
			score = sum(have)
			if score > maxScore:
				maxScore = score
	print(maxScore)

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
from collections import deque, _heapq
N, K = map(int, input().split())
V = deque(map(int, input().split()))

class Money:
    def __init__(self):
        self.yen = 0
        self.red = []

    def value(self):
        return self.yen + sum(self.red)

    def add(self, yen):
        if yen >= 0:
            self.yen += yen
        else:
            self.red.append(yen)

    def remove(self, yen):
        if yen >= 0:
            self.yen -= yen
        else:
            self.red.remove(yen)

mny = Money()
def get(cnt, mny, lstop, rstop):
    ans = mny.value()

    if cnt <= 0:
        return mny.yen + sum(mny.red)

    if len(V) > 0:

        if not lstop:
            w = V.popleft()
            mny.add(w)
            ans = max(ans, get(cnt-1, mny, lstop, rstop))
            mny.remove(w)
            V.appendleft(w)

        if not rstop:
            w = V.pop()
            mny.add(w)
            ans = max(ans, get(cnt-1, mny, lstop, rstop))
            mny.remove(w)
            V.append(w)

    if len(mny.red) >= cnt:
        w = min(mny.red)
        mny.red.remove(w)

        V.appendleft(w)
        ans = max(ans, get(cnt-1, mny, lstop or True, rstop))
        V.popleft()

        V.append(w)
        ans = max(ans, get(cnt-1, mny, lstop, rstop or True))
        V.pop()

        mny.red.append(w)    

    return ans

print(get(K, Money(), False, False))

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np

def main():
	N, K = map(int,input().split())

	Vs = list(map(int,input().split()))

	allCandidates = []
	if K > N:
		K = N
	for i in range(int(K / 2) + 1):
		for one in range(0, K - i + 1):
			allCandidates.extend([[0] * one + [1] * (K - i - one) + [-1] * i])

	maxScore = 0
	for candidate in allCandidates:
		have = []
		zeroCount = 0
		oneCount = 0
		for k in candidate:
			if k == 0:
				try:
					have.append(Vs[0 + zeroCount])
					zeroCount += 1
				except:
					break
			if k == 1:
				try:
					have.append(Vs[-1 - oneCount])
					oneCount += 1
				except:
					break
			if k == -1:
				try:
					if have[np.argmin(have)] < 0:
						del have[np.argmin(have)]
				except:
					break
		score = sum(have)

		if score > maxScore:
			maxScore = score
	print(maxScore)



if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np

def main():
	N, K = map(int,input().split())

	Vs = list(map(int,input().split()))

	allCandidates = []
	for i in range(int(K / 2) + 1):
		for one in range(0, K - i + 1):
			allCandidates.extend([[0] * one + [1] * (K - i - one) + [-1] * i])

	maxScore = 0
	for candidate in allCandidates:
		have = []
		zeroCount = 0
		oneCount = 0
		for k in candidate:
			if k == 0:
				try:
					have.append(Vs[0 + zeroCount])
					zeroCount += 1
				except:
					break
			if k == 1:
				try:
					have.append(Vs[-1 - oneCount])
					oneCount += 1
				except:
					break
			if k == -1:
				try:
					if have[np.argmin(have)] < 0:
						del have[np.argmin(have)]
				except:
					break
		score = sum(have)

		if score > maxScore:
			maxScore = score
	print(maxScore)



if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np

def main():
	N, K = map(int,input().split())

	Vs = list(map(int,input().split()))

	allCandidates = []
	for i in range(int(K / 2) + 1):
		for one in range(0, K - i + 1):
			allCandidates.extend([[0] * one + [1] * (K - i - one) + [-1] * i])

	maxScore = 0
	for candidate in allCandidates:
		have = []
		zeroCount = 0
		oneCount = 0
		for k in candidate:
			if k == 0:
				try:
					have.append(Vs[0 + zeroCount])
					zeroCount += 1
				except:
					break
			if k == 1:
				try:
					have.append(Vs[-1 - oneCount])
					oneCount += 1
				except:
					break
			if k == -1:
				try:
					del have[np.argmin(have)]
				except:
					break
		score = sum(have)

		if score > maxScore:
			maxScore = score
	print(maxScore)



if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=[int(i) for i in input().split()]
r=min(n,k)
ans=0
for a in range(r+1):
    for b in range(r-a+1):
        if b>=1:
            q=v[:a]+v[-b:]
        else:
            q=v[:a]
        t=r-a-b
        m=0
        for i in sorted(q):
            if i<0 and t>0:
                m+=abs(i)
                t-=1
        ans=max(ans,m+sum(q))
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np

def main():
	N, K = map(int,input().split())

	Vs = list(map(int,input().split()))

	allCandidates = []
	for i in range(int(K / 2) + 1):
		for one in range(0, K - i + 1):
			allCandidates.extend([[0] * one + [1] * (K - i - one) + [-1] * i])

	maxScore = 0
	for candidate in allCandidates:
		have = []
		zeroCount = 0
		oneCount = 0
		for k in candidate:
			if k == 0:
				try:
					have.append(Vs[0 + zeroCount])
					zeroCount += 1
				except:
					continue
			if k == 1:
				try:
					have.append(Vs[-1 - oneCount])
					oneCount += 1
				except:
					continue
			if k == -1:
				try:
					del have[np.argmin(have)]
				except:
					continue
		score = sum(have)

		if score > maxScore:
			maxScore = score
	print(maxScore)



if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np

def main():
	N, K = map(int,input().split())

	Vs = list(map(int,input().split()))

	allCandidates = []
	for i in range(int(K / 2) + 1):
		for one in range(0, K - i + 1):
			allCandidates.extend([[0] * one + [1] * (K - i - one) + [-1] * i])

	maxScore = 0
	for candidate in allCandidates:
		have = []
		zeroCount = 0
		oneCount = 0
		for k in candidate:
			if k == 0:
				try:
					have.append(Vs[0 + zeroCount])
					zeroCount += 1
				except:
					break
			if k == 1:
				try:
					have.append(Vs[-1 - oneCount])
					oneCount += 1
				except:
					break
			if k == -1:
				del have[np.argmin(have)]
		score = sum(have)

		if score > maxScore:
			maxScore = score
	print(maxScore)



if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
def value(k1,k2,k,D):
    if k2 == 0:
        v0 = D[:k1] + [0]
    else:
        v0 = D[:k1] + D[-(k2):] + [0]
    v0.sort()  # 取り出す宝石の価値リスト
    n_neg = v0.index(0)  # 負価値の数
    k0 = k - (k1+k2)  # 残り操作回数
    if n_neg <= k0:
        ans = sum(v0[n_neg:])
    else:
        ans = sum(v0[k0:])
    #print(k1,k2,n,k0,v0, ans)
    return ans

n,k = map(int,input().split(' '))
D = list(map(int,input().split(' ')))
score = 0
k1 = 0
while k1 < (k+1) and k1 < (n+1):
    k2 = 0
    while k2 < (k-k1) and k2 < (n+1-k1):
        v = value(k1,k2,k,D)
        if v > score:
            score = v
            #print(k1,k2)
            #print(score)
        k2 += 1
    k1 += 1
print(score)

Submission status: Wrong Answer

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

ans = 0
for k in range(1,K):#K+1だっけ？
    for i in range(k+1):
        R = V[-(k-i):]
        if len(R) == N:
            R = []
        # print(R)
        L = V[:i]+R+[0]
        L.sort()
        zero_index = min(L.index(0),KK-k)
        res = sum(L[zero_index:])
        ans = max(ans,res)
print(ans)