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

for i in range(1, K + 1):#i個以下取り出し、K - i個以下戻す＃１００
  nownow = 0
  for p in range(i + 1):#[0:i] から [N - i:N]まで取り出さないところを選べる#100
    #[p:p + N - i]を取り出さない
    now = sorted(V[:p] + V[p + N - i:])
    #print(now, i, p)
    index = bisect.bisect_right(now, 0)
    #index個の負の数がある。
    if K - i >= index:
      nownow = sum(now[index:])
    else:
      nownow = sum(now[K - i:])
    ans = max(ans, nownow)
    #print(now, nownow, 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(1, K + 1):#i個以下取り出し、K - i個以下戻す＃１００
  nownow = 0
  for p in range(i + 1):#[0:i] から [N - i:N]まで取り出さないところを選べる#100
    #[p:p + N - i]を取り出さない
    now = sorted(V[:p] + V[p + N - i:])
    #print(now, i, p)
    index = bisect.bisect_left(now, 0)
    #index個の負の数がある。
    if K - i >= index:
      nownow = sum(now[index:])
    else:
      nownow = sum(now[K - i:])
    ans = max(ans, nownow)
    #print(now, nownow, ans)

print(ans)

Submission status: Wrong Answer

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

# 1<=N<=50
# 1<=K<=100
# 10**8になってしまうので、全探索は無理そう？？？？


# CDは最後に持ってくれば良い。途中でやる必要はない
# A,Bをそれぞれ何回やるか決めれば、価値はわかるのでは。ただし、A,Bの数を合わせてKにならない場合もあり、その場合はリリースしないといけない
ans = 0

for i in range(K + 1):
    for j in range(K - i + 1):
        release_count = K - i - j
        # 抜き出す動作がNを超えてしまった時、入れて取り出すという作業によって暇つぶしをする必要がある。
        # print(i, j, release_count)
        if (i+j) < release_count:
          continue
        if i + j > N:
            nums = values.copy()
            # print(nums)
            # 暇つぶしのためのコスト
            if release_count < (i + j) - N:
                continue
            release_count -= (i + j) - N
            if release_count < 0:
                # 実現不可能なのでパス
                continue
            else:
                nums.sort()
                # print(sum(nums[release_count]))
                if len(nums) < release_count:
                    continue
                else:
                    ans = max(ans, sum(nums[release_count:]))
        elif i + j == N:
            nums = values.copy()
            ans = max(ans, sum(nums[release_count:]))
        else:
            nums = values[0:i].copy()
            right_nums = values[N - j:N].copy()
            nums.extend(right_nums)
            # print(nums)
            nums.sort()
            if len(nums) < release_count:
                continue
            # print(sum(nums[release_count:]))
            ans = max(ans, sum(nums[release_count:]))
print(ans)

Submission status: Wrong Answer

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

# 1<=N<=50
# 1<=K<=100
# 10**8になってしまうので、全探索は無理そう？？？？


# CDは最後に持ってくれば良い。途中でやる必要はない
# A,Bをそれぞれ何回やるか決めれば、価値はわかるのでは。ただし、A,Bの数を合わせてKにならない場合もあり、その場合はリリースしないといけない
ans = 0

for i in range(K + 1):
    for j in range(K - i + 1):
        release_count = K - i - j
        # 抜き出す動作がNを超えてしまった時、入れて取り出すという作業によって暇つぶしをする必要がある。
        # print(i, j, release_count)
        if i + j > N:
            nums = values.copy()
            # print(nums)
            # 暇つぶしのためのコスト
            if release_count < (i + j) - N:
                continue
            release_count -= (i + j) - N
            if release_count < 0:
                # 実現不可能なのでパス
                continue
            else:
                nums.sort()
                # print(sum(nums[release_count]))
                if len(nums) < release_count:
                    continue
                else:
                    ans = max(ans, sum(nums[release_count:]))
        elif i + j == N:
            nums = values.copy()
            ans = max(ans, sum(nums[release_count:]))
        else:
            nums = values[0:i].copy()
            right_nums = values[N - j:N].copy()
            nums.extend(right_nums)
            # print(nums)
            nums.sort()
            if len(nums) < release_count:
                continue
            # print(sum(nums[release_count:]))
            ans = max(ans, sum(nums[release_count:]))
print(ans)

Submission status: Wrong Answer

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

# 1<=N<=50
# 1<=K<=100
# 10**8になってしまうので、全探索は無理そう？？？？


# CDは最後に持ってくれば良い。途中でやる必要はない
# A,Bをそれぞれ何回やるか決めれば、価値はわかるのでは。ただし、A,Bの数を合わせてKにならない場合もあり、その場合はリリースしないといけない
ans = 0

for i in range(K + 1):
    for j in range(K - i + 1):
        release_count = K - i - j
        # 抜き出す動作がNを超えてしまった時、入れて取り出すという作業によって暇つぶしをする必要がある。
        # print(i, j, release_count)
        if i + j > N:
            nums = values.copy()
            # print(nums)
            # 暇つぶしのためのコスト
            release_count -= (i + j) - N
            if release_count < 0:
                # 実現不可能なのでパス
                continue
            elif release_count == 0:
                # print(sum(nums))
                ans = max(ans, sum(nums))
            else:
                nums.sort()
                # print(sum(nums[release_count]))
                ans = max(ans, sum(nums[release_count:]))
        else:
            nums = values[0:i].copy()
            right_nums = values[N - j:N].copy()
            nums.extend(right_nums)
            # print(nums)
            nums.sort()
            if len(nums) < release_count:
                continue
            # print(sum(nums[release_count:]))
            ans = max(ans, sum(nums[release_count:]))
print(ans)

Submission status: Wrong Answer

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

# 1<=N<=50
# 1<=K<=100
# 10**8になってしまうので、全探索は無理そう？？？？


# CDは最後に持ってくれば良い。途中でやる必要はない
# A,Bをそれぞれ何回やるか決めれば、価値はわかるのでは。ただし、A,Bの数を合わせてKにならない場合もあり、その場合はリリースしないといけない
ans = 0

for i in range(K):
    for j in range(K - i):
        release_count = K - i - j
        # 抜き出す動作がNを超えてしまった時、入れて取り出すという作業によって暇つぶしをする必要がある。
        # print(i, j, release_count)
        if i + j > N:
            nums = values.copy()
            # print(nums)
            # 暇つぶしのためのコスト
            release_count -= (i + j) - N
            if release_count < 0:
                # 実現不可能なのでパス
                continue
            elif release_count == 0:
                # print(sum(nums))
                ans = max(ans, sum(nums))
            else:
                nums.sort()
                # print(sum(nums[release_count]))
                ans = max(ans, sum(nums[release_count:]))
        else:
            nums = values[0:int(i)].copy()
            right_nums = values[N - j:N].copy()
            nums.extend(right_nums)
            # print(nums)
            nums.sort()
            if len(nums) < release_count:
                continue
            # print(sum(nums[release_count:]))
            ans = max(ans, sum(nums[release_count:]))
print(ans)

Submission status: Wrong Answer

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

# 1<=N<=50
# 1<=K<=100
# 10**8になってしまうので、全探索は無理そう？？？？


# CDは最後に持ってくれば良い。途中でやる必要はない
# A,Bをそれぞれ何回やるか決めれば、価値はわかるのでは。ただし、A,Bの数を合わせてKにならない場合もあり、その場合はリリースしないといけない
ans = 0

for i in range(K):
    for j in range(K - i):
        release_count = K - i - j
        # 抜き出す動作がNを超えてしまった時、入れて取り出すという作業によって暇つぶしをする必要がある。
        # print(i, j, release_count)
        if i + j > N:
            nums = values.copy()
            # print(nums)
            # 暇つぶしのためのコスト
            release_count -= N - (i + j)
            if release_count < 0:
                # 実現不可能なのでパス
                continue
            elif release_count == 0:
                # print(sum(nums))
                ans = max(ans, sum(nums))
            else:
                nums.sort()
                # print(sum(nums[release_count]))
                ans = max(ans, sum(nums[release_count:]))
        else:
            nums = values[0:int(i)].copy()
            right_nums = values[N - j:N].copy()
            nums.extend(right_nums)
            # print(nums)
            nums.sort()
            if len(nums) < release_count:
                continue
            # print(sum(nums[release_count:]))
            ans = max(ans, sum(nums[release_count:]))
print(ans)

Submission status: Wrong Answer

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

ans = - 10 ** 19
# i: Dから取り出す宝石の個数
for i in range(k + 1):
  # j: 左からとる個数(i-jが右からとる個数)
  for j in range(i + 1):
    inhands = V[:j] + V[n - i + j:]
    inhands.sort()
    remnum = k - i
    val = 0
    for h in inhands:
      if h < 0 and remnum > 0:
        remnum -= 1
      else:
        val += h
    ans = max(ans, val)
print(ans)

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


#############
# Main Code #
#############

N, K = getNM()
V = getList()

V = 2 * V

ans = 0
for i in range(1, K + 1):
    slow = K - i
    for j in range(N - i, N + 1):
        opt = sorted(V[j: j + i])
        for l in range(min(len(opt), slow)):
            if opt[l] < 0:
                opt[l] -= opt[l]
        ans = max(ans, sum(opt))
print(ans)

Submission status: Wrong Answer

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


ans = -1

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = [int(i) for i in input().split()]
ans = 0
for M in range(K+1):
    for m in range(M+1):
        ls = []
        cur1 = 0
        for i, x in enumerate(V):
            if i < m:
                ls.append(x)
                cur1 += 1
        cur2 = 0
        for i, x in enumerate(V[::-1]):
            if i < min(M-m, N-cur1):
                ls.append(x)
                cur2 += 1
        ls.sort()
        s = sum(ls)
        for i, x in enumerate(ls):
            if i < K-(cur1+cur2):
                s -= x
        ans = max(ans, s)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = [int(i) for i in input().split()]
ans = 0
for M in range(K+1):
    for m in range(M+1):
        ls = []
        for i, x in enumerate(V):
            if i < m:
                ls.append(x)
        for i, x in enumerate(V[::-1]):
            if i < min(M-m, N-m):
                ls.append(x)
        ls.sort()
        s = sum(ls)
        for i, x in enumerate(ls):
            if i < K-M:
                s -= x
        ans = max(ans, s)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = [int(i) for i in input().split()]
ans = 0
for M in range(K+1):
    for m in range(M+1):
        ls = []
        for i, x in enumerate(V):
            if i < m:
                ls.append(x)
        for i, x in enumerate(V[::-1]):
            if i < M-m:
                ls.append(x)
        ls.sort()
        s = sum(ls)
        for i, x in enumerate(ls):
            if i < K-M:
                s -= x
        ans = max(ans, s)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
*q,=map(int,input().split())

ans=0
for i in range(1,k+1):
    if i<=n:
        for left in range(0,i+1):
            tmp=0
            tmpq=[]
            right=i-left
            if right==0:
                right=-n
            tmpq+=q[:left]+q[-right:]
            tmp+=sum(tmpq)
            tmpq=sorted(tmpq)
            tmp-=sum(tmpq[:min(k-i, n)])
            ans=max(ans,tmp)
    else:
        tmp=0
        tmpq=q+[]
        tmp+=sum(tmpq)
        tmpq=sorted(tmpq)
        tmp-=sum(tmpq[:min(i-n, n)])
        ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
*q,=map(int,input().split())

ans=0
for i in range(1,k+1):
    if i<n:
        for left in range(0,i+1):
            tmp=0
            tmpq=[]
            right=i-left
            if right==0:
                right=-n
            tmpq+=q[:left]+q[-right:]
            tmp+=sum(tmpq)
            tmpq=sorted(tmpq)
            tmp-=sum(tmpq[:min(k-i, n)])
            ans=max(ans,tmp)
    else:
        for left in range(0,n+1):
            tmp=0
            tmpq=[]
            right=n-left
            if right==0:
                right=-n
            tmpq+=q[:left]+q[-right:]
            tmp+=sum(tmpq)
            tmpq=sorted(tmpq)
            tmp-=sum(tmpq[:min(i-n, n)])
            ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
*q,=map(int,input().split())

ans=0
for i in range(1,k+1):
    if i<=n:
        for left in range(0,i+1):
            tmp=0
            tmpq=[]
            right=max(i-left,0)
            if right==0:
                right=-n
            tmpq+=q[:left]+q[-right:]
            tmp+=sum(tmpq)
            tmpq=sorted(tmpq)
            tmp-=sum(tmpq[:min(k-i, n)])
            ans=max(ans,tmp)
    else:
        for left in range(0,n+1):
            tmp=0
            tmpq=[]
            right=max(n-left,0)
            if right==0:
                right=-n
            tmpq+=q[:left]+q[-right:]
            tmp+=sum(tmpq)
            tmpq=sorted(tmpq)
            tmp-=sum(tmpq[:min(k-n, n)])
            ans=max(ans,tmp)
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(n):
        if i + j <= k and i + j <= n:
            g = v[:i] + v[n-j:]
            g.sort()
            for l in range(min(k-i-j, i+j)+1):
                ans = max(ans, sum(g[l:]))

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(n):
        if i + j <= k and i + j <= n:
            g = v[:i] + v[n-j:]
            g.sort()
            print(g, min(k-i-j, i+j))
            for l in range(min(k-i-j, i+j)+1):
                ans = max(ans, sum(g[l:]))

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(n):
        if i + j <= k and i + j <= n:
            g = v[:i] + v[n-j:]
            g.sort()
            for l in range(1, min(k-i-j, i+j)+1):
                ans = max(ans, sum(g[l:]))

print(ans)

Submission status: Wrong Answer

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

c_max = 0

ans = []

# きっちりK回しなくていいみたい
for left in range(K + 1):
    for right in range(K + 1):
        if left + right > K:
            break

        li = V[:left] + V[N - right :]
        li = sorted(li)

        d = K - left - right

        c = sum(li)
        for i in range(min(d, len(li))):
            if li[i] < 0:
                c -= li[i]
            else:
                break

        ans.append(c)


print(max(ans))

Submission status: Wrong Answer

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

c_max = 0

ans = []

# きっちりK回しなくていいみたい
for left in range(K + 1):
    for right in range(K + 1):
        if left + right > K:
            break

        if right != 0:
            li = V[:left] + V[-right:]
        else:
            li = V[:left]

        li = sorted(li)

        d = K - left - right

        c = sum(li)
        for i in range(min(d, len(li))):
            if li[i] < 0:
                c -= li[i]
            else:
                break

        ans.append(c)


print(max(ans))

Submission status: Wrong Answer

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

c_max = 0

# きっちりK回しなくていいみたい
for left in range(K + 1):
    for right in range(K + 1):
        if left + right > K:
            break

        if right != 0:
            li = V[:left] + V[-right:]
        else:
            li = V[:left]

        li = sorted(li)

        d = K - left - right

        c = sum(li)
        for i in range(min(d, len(li))):
            if li[i] < 0:
                c -= li[i]
            else:
                break

        if c > c_max:
            c_max = c


print(c_max)

Submission status: Wrong Answer

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

c_max = 0

# きっちりK回しなくていいみたい
for left in range(K + 1):
    for right in range(K + 1):
        if left + right > K:
            break

        if right != 0:
            li = V[:left] + V[-right:]
        else:
            li = V[:left]

        li = sorted(li)

        d = K - left - right

        c = sum(li)
        for i, p in enumerate(li):
            if i < d and p < 0:
                c -= p

        if c > c_max:
            c_max = c


print(c_max)

Submission status: Wrong Answer

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

c_max = 0

# きっちりK回しなくていいみたい
for left in range(N + 1):
    for right in range(N + 1):
        if left + right > K:
            break

        if right != 0:
            li = V[:left] + V[-right:]
        else:
            li = V[:left]

        li = sorted(li)

        d = K - left - right
        c = sum(li[d:])

        if c > c_max:
            c_max = c


print(c_max)

Submission status: Wrong Answer

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

c_max = 0
for i in range(K+1):  # 操作AとBの回数を足したもの
    for a in range(i + 1):  # 操作Aの回数
        b = i - a  # 操作Bの回数
        if b != 0:
            li = V[:a] + V[-b:]
        else:
            li = V[:a]
        li = sorted(li)

        d = K - i  # 捨てる回数
        c = sum(li[d:])

        if c > c_max:
            c_max = c

print(c_max)

Submission status: Wrong Answer

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

c_max = 0
for i in range(K):  # 操作AとBの回数を足したもの
    for a in range(i + 1):  # 操作Aの回数
        b = i - a  # 操作Bの回数
        if b != 0:
            li = V[:a] + V[-b:]
        else:
            li = V[:a]
        li = sorted(li)

        d = K - a - b  # 捨てる回数
        c = sum(li[d:])

        if c > c_max:
            c_max = c

print(c_max)

Submission status: Wrong Answer

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

c_max = 0
for i in range(1, K):  # 操作AとBの回数を足したもの
    for a in range(i + 1):  # 操作Aの回数
        b = i - a  # 操作Bの回数
        if b != 0:
            li = V[:a] + V[-b:]
        else:
            li = V[:a]
        li = sorted(li)

        d = K - a - b  # 捨てる回数
        c = sum(li[d:])

        if c > c_max:
            c_max = c

print(c_max)

Submission status: Wrong Answer

Solution code:
"""
左からとった個数と右からとった個数の合計がmin(n, k)となることに注意して、全てのパターンについて試す
"""

import sys
sys.setrecursionlimit(10**6)

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

vr = v[::-1]

ans = 0

mi = min(n,k)

for i in range(mi+1):
    for j in range(mi-i+1):
        minus = 0 # 後で取り除くマイナスの値の合計
        now = v[:i]+vr[:j]
        d = k-(len(now)) # 操作できる残り回数。マイナスを取り除いて合計値を上げるために行う
        now.sort()
        md = min(d, len(now))
        for s in range(md):
            if 0 <= now[s]:
                break
            minus += now[s]
    ans = max(ans, sum(now)-minus)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=[int(x) for x in input().rstrip().split()]
v=[int(x) for x in input().rstrip().split()]
vr=Vrev = v[::-1]
ans=0
mi=min(n,k)
ans_l=[]
for i in range(mi+1):
  for j in range(mi-i+1):
    minus=0
    now=v[:i]+vr[:j]
    d=mi-(len(now))
    now.sort()
    md=min(d,len(now))
    for s in range(md):
      if 0<=now[s]:
        break
      minus+=now[s]

    ans=max(ans,sum(now)-minus)
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
import copy
N,K = map(int, input().split())
V_list= list(map(int, input().split()))

selected = []
left = []

max_sum = max(V_list[0], V_list[-1])
left_sum = 0
for l in range(N):
    selected = copy.deepcopy(left)
    lr_sum = left_sum
    for r in range(N-l):
        # skip
        stock = K-l-r
        if stock < 0:
            break
        ri = N-r-1
        select_cnt = len(selected)
        tmp_sum = lr_sum
        max_sum = max(max_sum, tmp_sum)
        popper = copy.deepcopy(selected)

        for i in range(1, stock+1):
            if i > select_cnt:
                break
            tmp_sum -= heapq.heappop(popper)
            max_sum = max(max_sum, tmp_sum)

        r_val = V_list[ri]
        lr_sum += r_val
        heapq.heappush(selected, r_val)

    l_val = V_list[l]
    left_sum += l_val
    heapq.heappush(left, l_val)

print(max_sum)

Submission status: Wrong Answer

Solution code:
# 初期入力
import bisect
import sys
input = sys.stdin.readline
N,K = (int(i) for i in input().split())
V = list(map(int, input().split()))
V_sorted =sorted(V)

#取る個数t、返す個数ｒ(t +r <=K)個全て決める

take_plus =[]
take_minus =[]
ans =[]
take_V =[] #最後に持っている宝石の価値
#Vが全てマイナスなら取らない
if V_sorted[-1] <=0:
    ans =0
else:
    take_num =min(K+1,N+1)
    for tak in range(1,take_num):
        reverse =min(take_num -1 -tak,tak)
        for lef in range(tak+1):
            for rev in range(reverse +1):
            
                take =[]                    #取った宝石
                rig =N-(tak -lef )          #lef:左側の取る数 rig:右側の取る数
                take.extend(V[    :lef])
                take.extend(V[rig:   ])
                take.sort()
                take_V.append(sum(take[rev:]))
        
    ans =max(take_V)
print(ans)

Submission status: Wrong Answer

Solution code:
# 初期入力
import bisect
import sys
input = sys.stdin.readline
N,K = (int(i) for i in input().split())
V = list(map(int, input().split()))
V_sorted =sorted(V)

#取る個数t、返す個数ｒ(t +r <=K)個全て決める

take_plus =[]
take_minus =[]
ans =[]
take_V =[] #最後に持っている宝石の価値
#Vが全てマイナスなら取らない
if V_sorted[-1] <=0:
    ans =0
else:
    for tak in range(1,K+1):
        reverse =min(K -tak,tak)
        for lef in range(tak+1):
            for rev in range(reverse +1):
            
                take =[]                    #取った宝石
                rig =N-(tak -lef )          #lef:左側の取る数 rig:右側の取る数
                take.extend(V[    :lef])
                take.extend(V[rig:   ])
                take.sort()
                take_V.append(sum(take[rev:]))
        
    ans =max(take_V)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
targ = min(n, k)
v = list(map(int,input().split()))
"""
i…左を取る回数
j…右を取る回数
残り回数 k-(i+j)
"""
ans = -10**18
for i in range(targ):
	for j in range(targ-i+1):
		#print(i, j)
		nl = v[:i] + v[n-j:]
		nl.sort()
		#print(nl)
		tmp = sum(nl)
		for t in range(min(k-(i+j), i+j)):
			tmp = max(tmp, tmp-nl[t])
			#print("行いましたよ!")
		#print(tmp)
		ans = max(tmp, 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):
  for j in range(k+1):
    if i+j > k or i+j > n:
      continue
    t = k-(i+j)
    s = v[:i] + v[(n-j):]
    s.sort()
    u = 0
    print (s,t)
    while u < t:
      u += 1
      if len(s) < 1:
        break
      if s[0] < 0:
        s.pop(0)
      else:
        break
    ans = max(ans, sum(s))
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+1):
    x = i+1
    y = j+1
    if i+j > k:
      continue
    jewl = 0
    t = k-(i+j)
    s = v[:i] + v[(n-j):]
    s.sort()
    u = 0
    # print (s)
    while u < t:
      u += 1
      if len(s) < 1:
        break
      if s[0] < 0:
        s.pop(0)
      else:
        break
    ans = max(ans, sum(s))
print (ans)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
n,K = map(int,input().split())
V = list(map(int,input().split()))
num = min(n,K)
MAX = 0
for i in range(1,num+1):
    for j in range(i+1):
        left = j
        right = i - j
        dec = num - i
        v = []
        ans = 0
        for le in range(left):
            v.append(V[le])
            ans += V[le]
        for ri in range(right):
            v.append(V[n-1-ri])
            ans += V[n-1-ri]
        v.sort()
        dec = min(dec,len(v))
        for k in range(dec):
            if v[k] < 0:
                ans -= v[k]
            else:
                break
        MAX = max(ans,MAX)
print(MAX)

Submission status: Wrong Answer

Solution code:
import heapq

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

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

ans = 0
for i in range(1, K+1):
    if i > N:
        continue
    for j in range(i+1):
        tmp = 0
        q = V[:j] + V[N-i+j:]
        heapq.heapify(q)
        for _ in range(min(i, K-i)):
            tmp_min = heapq.heappop(q)
            ans = max(ans, sum(q))
            if tmp_min > 0:
                ans = max(ans, sum(q)+tmp_min)
                break

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

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

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

ans = 0
for i in range(K//2+K%2, K+1):
    if i > 50:
        continue
    for j in range(i+1):
        tmp = 0
        q = V[:j] + V[N-i+j:]
        heapq.heapify(q)
        for _ in range(K-i):
            tmp_min = heapq.heappop(q)
            ans = max(ans, sum(q))
            if tmp_min > 0:
                ans = max(ans, sum(q)+tmp_min)
                break

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)

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

    ans = 0
    for k in range(min(K+1,N+1)):
        for x in range(k+1):
            X = V[0:x]
            for y in range(k+1):
                Y = V[N-y:]
                z = k-(x+y)
                if z < 0: continue

                W = X + Y
                W.sort()
                ans = max(ans,sum(W[z:]))


    print(ans)



if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)

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

    X,Y = [],[]

    if N % 2 == 0:
        for i in range(N//2):
            v = V[i]
            X.append(v)

        for i in reversed(range(N//2,N)):
            v = V[i]
            Y.append(v)
    else:
        for i in range(N//2+1):
            v = V[i]
            X.append(v)

        for i in reversed(range(N//2+1,N)):
            v = V[i]
            Y.append(v)

    # print(X,Y)
    ans = 0
    len_of_X = len(X)
    len_of_Y = len(Y)
    for k in range(K+1):
        for x in range(len_of_X+1):
            _X = X[0:x]
            for y in range(len_of_Y+1):
                _Y = Y[0:y]
                z = k-(x+y)
                if z < 0: continue

                W = _X + _Y
                W.sort()
                ans = max(ans,sum(W[z:]))

                # print(k,_X,_Y,W)


    print(ans)





if __name__ == "__main__":
  main()