Problem:


Score : 400 points

Problem Statement

Your friend gave you a dequeue D as a birthday present.

D is a horizontal cylinder that contains a row of N jewels.

The values of the jewels are V_1, V_2, ..., V_N from left to right. There may be jewels with negative values.

In the beginning, you have no jewel in your hands.

You can perform at most K operations on D, chosen from the following, at most K times (possibly zero):

Operation A: Take out the leftmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

Operation B: Take out the rightmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

Operation C: Choose a jewel in your hands and insert it to the left end of D. You cannot do this operation when you have no jewel in your hand.

Operation D: Choose a jewel in your hands and insert it to the right end of D. You cannot do this operation when you have no jewel in your hand.

Find the maximum possible sum of the values of jewels in your hands after the operations.

Constraints

All values in input are integers.

1 \leq N \leq 50

1 \leq K \leq 100

-10^7 \leq V_i \leq 10^7

Input

Input is given from Standard Input in the following format:

N K
V_1 V_2 ... V_N

Output

Print the maximum possible sum of the values of jewels in your hands after the operations.

Sample Input 1

6 4
-10 8 2 1 2 6

Sample Output 1

14

After the following sequence of operations, you have two jewels of values 8 and 6 in your hands for a total of 14, which is the maximum result.

Do operation A. You take out the jewel of value -10 from the left end of D.

Do operation B. You take out the jewel of value 6 from the right end of D.

Do operation A. You take out the jewel of value 8 from the left end of D.

Do operation D. You insert the jewel of value -10 to the right end of D.

Sample Input 2

6 4
-6 -100 50 -2 -5 -3

Sample Output 2

44

Sample Input 3

6 3
-6 -100 50 -2 -5 -3

Sample Output 3

0

It is optimal to do no operation.



Sample input:
6 4
-10 8 2 1 2 6


Sample output:
14

Submission status: Wrong Answer

Solution code:
import sys
from io import StringIO
import unittest
import os

# 再帰処理上限(dfs作成時に設定するのが面倒なので限度近い値を組み込む)
sys.setrecursionlimit(999999999)


# 実装を行う関数
def resolve(test_def_name=""):
    n, k = map(int, input().split())

    v_s = list(map(int, input().split()))
    v_s_rev = list(reversed(v_s))

    ans = 0
    for i in range(min(n, k) + 1):
        left = i

        for j in range(min(n, k) + 1 - i):
            right = j
            remove = min(n, k) - i - j

            val_list = v_s[0:left] + v_s_rev[0:right]
            val_list.sort()
            for loop_k in range(remove):
                if len(val_list) == 0:
                    break
                if len(val_list) < loop_k + 1:
                    break
                if val_list[loop_k] >= 0:
                    break
                val_list[loop_k] = 0

            ans = max(ans, sum(val_list))

    print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """6 4
-10 8 2 1 2 6"""
        output = """14"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """6 4
-6 -100 50 -2 -5 -3"""
        output = """44"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """6 3
-6 -100 50 -2 -5 -3"""
        output = """0"""
        self.assertIO(test_input, output)

    def test_input_4(self):
        test_input = """6 1
-100 14 1 1 -1 10"""
        output = """10"""
        self.assertIO(test_input, output)

    def test_input_5(self):
        test_input = """6 2
-100 14 1 1 -1 10"""
        output = """10"""
        self.assertIO(test_input, output)

    def test_input_6(self):
        test_input = """6 3
-100 14 1 1 -1 10"""
        output = """14"""
        self.assertIO(test_input, output)

    def test_input_7(self):
        test_input = """6 4
-100 14 1 1 -1 10"""
        output = """24"""
        self.assertIO(test_input, output)

    def test_input_8(self):
        test_input = """6 5
-100 14 1 1 -1 10"""
        output = """25"""
        self.assertIO(test_input, output)

    def test_input_9(self):
        test_input = """6 6
-100 14 1 1 -1 10"""
        output = """26"""
        self.assertIO(test_input, output)

    def test_input_10(self):
        test_input = """6 7
-100 14 1 1 -1 10"""
        output = """26"""
        self.assertIO(test_input, output)

    def test_input_11(self):
        test_input = """5 1
-100 -100 1 -100 -100"""
        output = """0"""
        self.assertIO(test_input, output)

    def test_input_12(self):
        test_input = """5 2
-100 -100 1 -100 -100"""
        output = """0"""
        self.assertIO(test_input, output)

    def test_input_13(self):
        test_input = """5 3
-100 -100 1 -100 -100"""
        output = """0"""
        self.assertIO(test_input, output)

    def test_input_14(self):
        test_input = """5 4
-100 -100 1 -100 -100"""
        output = """0"""
        self.assertIO(test_input, output)

    def test_input_15(self):
        test_input = """5 5
-100 -100 1 -100 -100"""
        output = """1"""
        self.assertIO(test_input, output)


# 実装orテストの呼び出し
if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        # AtCoder提出時の場合
        resolve()

    else:
        # 自PCの場合
        unittest.main()

Submission status: Wrong Answer

Solution code:
import sys
from io import StringIO
import unittest
import os

# 再帰処理上限(dfs作成時に設定するのが面倒なので限度近い値を組み込む)
sys.setrecursionlimit(999999999)


# 実装を行う関数
def resolve(test_def_name=""):
    n, k = map(int, input().split())

    v_s = list(map(int, input().split()))
    v_s_rev = list(reversed(v_s))

    ans = 0
    for i in range(min(n, k) + 1):
        left = i

        for j in range(min(n, k) + 1 - i):
            right = j
            remove = min(n, k) - i - j

            val_list = v_s[0:left] + v_s_rev[0:right]
            val_list.sort()
            for loop_k in range(remove):
                if len(val_list) == 0:
                    break
                if len(val_list) < loop_k + 1:
                    break
                if val_list[loop_k] >= 0:
                    break
                val_list[loop_k] = 0

            ans = max(ans, sum(val_list))

    print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """6 4
-10 8 2 1 2 6"""
        output = """14"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """6 4
-6 -100 50 -2 -5 -3"""
        output = """44"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """6 3
-6 -100 50 -2 -5 -3"""
        output = """0"""
        self.assertIO(test_input, output)

    def test_input_4(self):
        test_input = """6 1
-100 14 1 1 -1 10"""
        output = """10"""
        self.assertIO(test_input, output)


# 実装orテストの呼び出し
if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        # AtCoder提出時の場合
        resolve()

    else:
        # 自PCの場合
        unittest.main()

Submission status: Wrong Answer

Solution code:
import sys
from io import StringIO
import unittest
import os

# 再帰処理上限(dfs作成時に設定するのが面倒なので限度近い値を組み込む)
sys.setrecursionlimit(999999999)


# 実装を行う関数
def resolve(test_def_name=""):
    n, k = map(int, input().split())

    v_s = list(map(int, input().split()))
    v_s_rev = list(reversed(v_s))

    ans = 0
    for i in range(min(n, k)):
        left = i

        for j in range(min(n, k) - i):
            right = j
            remove = min(n, k) - i - j

            val_list = v_s[0:left] + v_s_rev[0:right]
            val_list.sort()
            for loop_k in range(remove):
                if len(val_list) == 0:
                    break
                if len(val_list) < loop_k + 1:
                    break
                if val_list[loop_k] >= 0:
                    break
                val_list[loop_k] = 0

            ans = max(ans, sum(val_list))

    print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """6 4
-10 8 2 1 2 6"""
        output = """14"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """6 4
-6 -100 50 -2 -5 -3"""
        output = """44"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """6 3
-6 -100 50 -2 -5 -3"""
        output = """0"""
        self.assertIO(test_input, output)

    # 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)


# 実装orテストの呼び出し
if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        # AtCoder提出時の場合
        resolve()

    else:
        # 自PCの場合
        unittest.main()

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
l=list(map(int,input().split()))
ans=0
S=min(N,K)
if S==1:
   if max(l)>0:
      print(max(l))
      exit()
   else:
      print(0)
      exit()
for i in range(S):
   for j in range(S-i):
      tmp=sum(l[0:i])+sum(l[N-j:N])
      tmpl=sum([],l[0:i]+l[N-j:N])
      tmpl.sort()
      for k in range(min(N,K-i-j)):
         try:
            if tmpl[k]<0:
               tmp+=abs(tmpl[k])
         except IndexError:
            break
      ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
l=list(map(int,input().split()))
ans=0
S=min(N,K)
for i in range(S):
   for j in range(S-i):
      tmp=sum(l[0:i])+sum(l[N-j:N])
      tmpl=sum([],l[0:i]+l[N-j:N])
      tmpl.sort()
      for k in range(min(N,K-i-j)):
         try:
            if tmpl[k]<0:
               tmp+=abs(tmpl[k])
         except IndexError:
            break
      ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
l=list(map(int,input().split()))
ans=0
for i in range(K):
   for j in range(K-i):
      tmp=sum(l[0:i])+sum(l[N-j:N])
      tmpl=sum([],l[0:i]+l[N-j:N])
      tmpl.sort()
      for k in range(min(N,K-i-j)):
         try:
            if tmpl[k]<0:
               tmp+=abs(tmpl[k])
         except IndexError:
            break
      ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
l=list(map(int,input().split()))
tmpl=[i for i in l if i<0]
K=min(K,N+len(tmpl))
ans=0
for i in range(K):
   for j in range(K-i):
      tmp=sum(l[0:i])+sum(l[N-j:N+1])
      tmp_l=sum([],l[0:i]+l[N-j:N+1])
      tmp_l.sort()
      for k in range(K-i-j):
         try:
            if tmp_l[k]<0:
               tmp+=abs(tmp_l[k])
         except IndexError:
            break
      ans=max(tmp,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
l=list(map(int,input().split()))
K=min(N,K)
ans=0
for i in range(K):
   for j in range(K-i):
      tmp=sum(l[0:i])+sum(l[N-j:N+1])
      tmp_l=sum([],l[0:i]+l[N-j:N+1])
      tmp_l.sort()
      for k in range(K-i-j):
         try:
            if tmp_l[k]<0:
               tmp+=abs(tmp_l[k])
         except IndexError:
            break
      ans=max(tmp,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
l=list(map(int,input().split()))
ans=0
for i in range(K):
   for j in range(K-i):
      tmp=sum(l[0:i])+sum(l[N-j:N+1])
      tmp_l=sum([],l[0:i]+l[N-j:N+1])
      tmp_l.sort()
      for k in range(K-i-j):
         try:
            if tmp_l[k]<0:
               tmp+=abs(tmp_l[k])
         except IndexError:
            break
      ans=max(tmp,ans)
print(ans)

Submission status: Wrong Answer

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

max_val = 0
left_hq = list()
left_val = 0
for left in range(N+1):
    if left > 0:
        heapq.heappush(left_hq,V[left-1])
        left_val += V[left-1]
        all_hq = copy.deepcopy(left_hq)
        all_val = left_val
        for right in range(N+1):
            if right+left > K or right+left > N :
                continue
            if right > 0:
                heapq.heappush(all_hq,V[-right])
                all_val += V[-right]
            
            temp_hq = copy.deepcopy(all_hq)
            temp_val = all_val
            for i in range(K-left-right):
                if len(temp_hq) == 0:
                    break
                else:
                    minus = heapq.heappop(temp_hq)
                    temp_val -= minus
            if temp_val > max_val:
                max_val = temp_val
print(max_val)

Submission status: Wrong Answer

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

max_val = 0
left_hq = list()
left_val = 0
for left in range(N+1):
    if left > 0:
        heapq.heappush(left_hq,V[left-1])
        left_val += V[left-1]
        all_hq = copy.deepcopy(left_hq)
        all_val = left_val
        for right in range(N+1):
            if right+left > min(K,N):
                continue
            if right > 0:
                heapq.heappush(all_hq,V[-right])
                all_val += V[-right]
            
            temp_hq = copy.deepcopy(all_hq)
            temp_val = all_val
            for i in range(K-left-right):
                if len(temp_hq) == 0:
                    break
                else:
                    minus = heapq.heappop(temp_hq)
                    temp_val -= minus
            if temp_val > max_val:
                max_val = temp_val
print(max_val)

Submission status: Wrong Answer

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

max_val = 0
left_hq = list()
left_val = 0
for left in range(N+1):
    if left > 0:
        heapq.heappush(left_hq,V[left-1])
        left_val += V[left-1]
        all_hq = copy.deepcopy(left_hq)
        all_val = left_val
        for right in range(N+1):
            if right+left > N or right+left > K:
                continue
            if right > 0:
                heapq.heappush(all_hq,V[-right])
                all_val += V[-right]
            
            temp_hq = copy.deepcopy(all_hq)
            temp_val = all_val
            for i in range(min(N,K)-left-right):
                if len(temp_hq) == 0:
                    break
                else:
                    minus = heapq.heappop(temp_hq)
                    temp_val -= minus
            if temp_val > max_val:
                max_val = temp_val
print(max_val)

Submission status: Wrong Answer

Solution code:

from collections import deque

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

ans = 0
for a in range(K):
    for b in range(K - a):
        if a + b > N:
            continue

        q = deque(X)
        tmp = []
        for _ in range(a):
            tmp.append(q.popleft())

        for _ in range(b):
            tmp.append(q.pop())

        tmp.sort(reverse=True)
        cnt = 0
        for _ in range(K - a - b):
            if tmp and tmp[-1] < 0:
                tmp.pop()

        ans = max(ans, sum(tmp))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline


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

    ans = -float("inf")
    for t in range(min(N, K)):
        s = K - t
        for l in range(t + 1):
            r = t - l
            gem = V[:l]
            gem += V[-r:] if r != 0 else []
            gem.sort()
            value = sum(gem)
            for i in range(min(s, t)):
                if gem[i] < 0:
                    value -= gem[i]
                else:
                    break
            ans = max(ans, value)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

from collections import deque
V = deque(V)

import copy

ans = -10**18
for i in range(k+1):
    for j in range(k+1):
        if i+j > k:
            continue
        if i+j < k-(i+j):
            continue
        if i+j > n:
            continue
        V_ = copy.copy(V)
        temp = []
        for p in range(i):
            temp.append(V_.popleft())
        for q in range(j):
            temp.append(V_.pop())
        temp.sort()
        s = sum(temp)
        l = k-i-j
        for r in range(l):
            if temp[r] < 0:
                s -= temp[r]
        ans = max(ans, s)
print(max(ans, 0))

Submission status: Wrong Answer

Solution code:
def solve():
  ans = 0
  N, K = map(int, input().split())
  V = list(map(int, input().split()))
  A = V[:]*2
  for k in range(K,K//2,-1):
    if k>=N:
      suteru = k-N
      a = V[:]
      a.sort()
      ans = max(ans,sum(a[suteru:]))
    else:
      suteru = K-k
      for i in range(k+1):
        a = A[N-i:N+k-i]
        a.sort()
        ans = max(ans,sum(a[suteru:]))
  return ans
print(solve())

Submission status: Wrong Answer

Solution code:
def solve():
  ans = 0
  N, K = map(int, input().split())
  V = list(map(int, input().split()))
  A = V[:]*2
  for k in range(K,K//2,-1):
    if k>N:
      continue
    suteru = K-k
    if k==N:
      a = V[:]
      a.sort()
      ans = max(ans,sum(a[suteru:]))
    else:
      for i in range(k+1):
        a = A[N-i:N+k-i]
        a.sort()
        ans = max(ans,sum(a[suteru:]))
  return ans
print(solve())

Submission status: Wrong Answer

Solution code:
def solve():
  ans = 0
  N, K = map(int, input().split())
  V = list(map(int, input().split()))
  A = V[:]*2
  for k in range(K,K//2,-1):
    suteru = K-k
    if k>=N:
      a = V[:]
      a.sort()
      ans = max(ans,sum(a[suteru:]))
    else:
      for i in range(k+1):
        a = A[N-i:N+k-i]
        a.sort()
        ans = max(ans,sum(a[suteru:]))
  return ans
print(solve())

Submission status: Wrong Answer

Solution code:
def solve():
  ans = 0
  N, K = map(int, input().split())
  V = [(-1)**i for i in range(N)]
  A = V[:]*2
  for k in range(K,K//2,-1):
    suteru = K-k
    if k>=N:
      a = V[:]
      a.sort()
      ans = max(ans,sum(a[suteru:]))
    else:
      for i in range(k+1):
        a = A[N-i:N+k-i]
        a.sort()
        ans = max(ans,sum(a[suteru:]))
  return ans
print(solve())

Submission status: Wrong Answer

Solution code:
def solve():
  ans = 0
  N, K = map(int, input().split())
  V = list(map(int, input().split()))
  A = V[:]*2
  for k in range(K,K//2,-1):
    suteru = K-k
    if k>=50:
      a = V[:]
      a.sort()
      ans = max(ans,sum(a[suteru:]))
    else:
      for i in range(k+1):
        a = A[N-i:N+k-i]
        a.sort()
        ans = max(ans,sum(a[suteru:]))
  return ans
print(solve())

Submission status: Wrong Answer

Solution code:
def solve():
  ans = 0
  N, K = map(int, input().split())
  V = list(map(int, input().split()))
  A = V[:]*2
  for k in range(K,0,-1):
    suteru = K-k
    if k>=50:
      a = V[:]
      a.sort()
      ans = max(ans,sum(a[suteru:]))
    else:
      for i in range(k+1):
        a = A[N-i:N+k-i]
        a.sort()
        ans = max(ans,sum(a[suteru:]))
  return ans
print(solve())

Submission status: Wrong Answer

Solution code:
import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

N, K = rl()
V = rl()
dp = [[[-float('inf')]*(K+1) for _ in range(N+1)] for _ in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
	for j in range(N):
		for k in range(K):
			dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k])
			# if i+j >= K or i+j >= N:
			# 	print('?', i,j,k,dp[i][j][k+1], dp[i][j][k])
			# 	dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k])
			# 	continue
			ni, nj, nk = i+1, j, k+1
			if ni+nj <= N:
				dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[i])
				if V[i] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k])
			ni, nj, nk = i, j+1, k+1
			if ni+nj <= N:
				dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[N-j-1])
				if V[N-j-1] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k])
print(dp)
ans = 0
for i in range(N+1):
	for j in range(N+1):
		ans = max(ans, dp[i][j][K])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

N, K = rl()
V = rl()
dp = [[[-float('inf')]*(K+1) for _ in range(N)] for _ in range(N)]
dp[0][0][0] = 0

for i in range(N-1):
	for j in range(N-1):
		for k in range(K):
			if i+j >= K:
				dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k])
				continue
			ni, nj, nk = i+1, j, k+1
			if ni+nj <= N:
				dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[i])
				if V[i] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k])
			ni, nj, nk = i, j+1, k+1
			if ni+nj <= N:
				dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[N-j-1])
				if V[N-j-1] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k])

ans = 0
for i in range(N):
	for j in range(N):
		ans = max(ans, dp[i][j][K])
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n, k = map(int, input().split())
vs = list(map(int, input().split()))
res = 0
for i in range(min(n,k)+1):
    for j in range(min(n,k-i)+1):
        res_tmp = 0
        arr = []
        if i > 0:
            arr += vs[:i]
        if j > 0:
            arr += vs[(n-j):]
        if arr != []:
            arr.sort()
            n_len = len(arr)
            for m in range(min(k-i-j,n_len)):
                if arr[m] >= 0:
                    break
                res_tmp -= arr[m]
            res_tmp += sum(arr)
        res = max(res,res_tmp)
print(res)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n, k = map(int, input().split())
vs = list(map(int, input().split()))
res = 0
for i in range(min(n,k)+1):
    for j in range(min(n,k-i)+1):
        res_tmp = 0
        arr = []
        if i > 0:
            arr += vs[:i]
        if j > 0:
            arr += list(reversed(vs))[:j]
        if arr != []:
            arr.sort()
            n_len = len(arr)
            for m in range(min(k-i-j,n_len)):
                if arr[m] >= 0:
                    break
                res_tmp -= arr[m]
            res_tmp += sum(arr)
        res = max(res,res_tmp)
print(res)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n, k = map(int, input().split())
vs = list(map(int, input().split()))
res = 0
for i in range(min(n,k)+1):
    for j in range(k-i+1):
        res_tmp = 0
        arr = []
        if i > 0:
            arr += vs[:i]
        if j > 0:
            arr += list(reversed(vs))[:j]
        if arr != []:
            arr.sort()
            n_len = len(arr)
            for m in range(min(k-i-j,n_len)):
                if arr[m] >= 0:
                    break
                res_tmp -= arr[m]
            res_tmp += sum(arr)
        res = max(res,res_tmp)
print(res)

Submission status: Wrong Answer

Solution code:
from collections import deque
import heapq
n,k = map(int,input().split())
v = deque(list(map(int,input().split())))

ans = 0
for i in range(min(n,k)+1):
    for j in range(min(n,k)-i):
        l = [0]
        r = [0]
        mm = [0]
        for p in range(i):
            if v[p] > 0:
                l.append(v[p])
            else:
                heapq.heappush(mm,v[p])
        for o in range(j):
            if v[-o-1] > 0:
                r.append(v[-o-1])
            else:
                heapq.heappush(mm,v[-o-1])
        for m in range(min(k-i-j,len(mm))):
            heapq.heappop(mm)
        # print(i,j,ans,l,r)
        ans = max(ans,sum(l)+sum(r)+sum(mm))
print(ans)
# while k and v:
#     heapq.heappush(l,deque.popleft(v))
#     k-=1
# ans = sum(l)+sum(r)
# while l:
#     print(ans)
#     deque.appendleft(v,heapq.heappop(l))
#     ans = max(ans,sum(l)+sum(r))
#     heapq.heappush(r,deque.pop(v))
#     ans = max(ans,sum(l)+sum(r))
# print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import heapq
n,k = map(int,input().split())
v = deque(list(map(int,input().split())))

ans = 0
for i in range(min(n,k)):
    for j in range(min(n,k)-i):
        l = [0]
        r = [0]
        mm = [0]
        for p in range(i):
            if v[p] > 0:
                l.append(v[p])
            else:
                heapq.heappush(mm,v[p])
        for o in range(j):
            if v[-o-1] > 0:
                r.append(v[-o-1])
            else:
                heapq.heappush(mm,v[-o-1])
        for m in range(min(k-i-j,len(mm))):
            heapq.heappop(mm)
        # print(i,j,ans,l,r)
        ans = max(ans,sum(l)+sum(r)+sum(mm))
print(ans)
# while k and v:
#     heapq.heappush(l,deque.popleft(v))
#     k-=1
# ans = sum(l)+sum(r)
# while l:
#     print(ans)
#     deque.appendleft(v,heapq.heappop(l))
#     ans = max(ans,sum(l)+sum(r))
#     heapq.heappush(r,deque.pop(v))
#     ans = max(ans,sum(l)+sum(r))
# print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7

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

    # 方針: 操作Aの回数・操作Bの回数で全探索
    ans = 0
    R = min(N,K)

    for i in range(R+1):
        for j in range(R+1-i):
            tmp = 0
            q = []# 価値が負の宝石をいれる(C)
            
            for k in range(i):
                tmp += V[k]
                if V[k] < 0:
                    q.append(V[k])
            for k in range(j):
                tmp += V[N-1-k]
                if V[N-1-k] < 0:
                    q.append(V[N-1-k])

            q.sort()
            tmp -= sum(q[:min(len(q),R-i-j)])
            # print(i,j,q,tmp)
            ans = max(ans,tmp)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import heapq
pop = heapq.heappop
push = heapq.heappush

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

ans = 0
for i in range(min(N + 1, K + 1)):
  for j in range(min(N + 1, K + 1) - i):
    q = []
    t = 0
    for k in range(i):
      tt = V[k]
      t += tt
      push(q, tt)
    for k in range(j):
      tt = V[N - 1 - k]
      t += tt
      push(q, tt)

    for k in range(K - i - j):
      if q == []:
        break
      tt = pop(q)
      if tt >= 0:
        break
      t -= tt

    print(i, j, k,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
pop = heapq.heappop
push = heapq.heappush

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

ans = 0
for i in range(min(N + 1, K)):
  for j in range(min(N + 1, K) - i):
    q = []
    t = 0
    for k in range(i):
      tt = V[k]
      t += tt
      push(q, tt)
    for k in range(j):
      tt = V[N - 1 - k]
      t += tt
      push(q, tt)

    for k in range(K - i):
      if q == []:
        break
      tt = pop(q)
      if tt >= 0:
        break
      t -= tt

    ans = max(ans, t)

print(ans)

Submission status: Wrong Answer

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

ans = 0
for x in range(k+1):# LeftHand
  for y in range(k-x+1):# RightHand
    temp1 = []
    temp2 = []
    for i in v[:x]:
      if i >= 0:
        temp1.append(i)
      else:
        temp2.append(i)
    for i in v[len(v)-y:]:
      if i >= 0:
        temp1.append(i)
      else:
        temp2.append(i)
    temp2.sort()
    ans = max(ans, sum(temp1) + sum(temp2[k-x-y:]))
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import product
def main():
    n, k = list(map(int, input().split()))
    V = list(map(int, input().split()))
    ans = -1
    for i, j in product(range(c := min(n, k)), repeat = 2):
        if i + j > c:
            continue
        A = V[:i] + V[-j:] if j != 0 else V[:i]
        A.sort(reverse = True)
        r = k - (i + j)
        for _ in range(r):
            if len(A) == 0:
                break
            if (a := A.pop()) >= 0:
                A.append(a)
                break
        ans = max(ans, sum(A))
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main(n,k,v):
    ruisekiL=[0]*(n+1)
    ruisekiR=[0]*(n+1)
    for i in range(1,n+1):
        ruisekiL[i]=ruisekiL[i-1]+v[i-1]
        ruisekiR[i]=ruisekiR[i-1]+v[n-i]
    vv=[]
    for i in range(n):
        vv.append((i+1,v[i]))
    vv.sort(key=lambda x:x[1])
    #print(vv)

    maxown=-10**7
    for kk in range(k+1):
        if n<kk:
            own=ruisekiL[n]
            m=0
            while m<n and vv[m][1]<0: 
                own-=vv[i][1]
                m+=1
            #print(own)
            maxown=max(maxown,own)
        else:
            for l in range(kk//2+1):
                for i in range(kk-l+1):
                    j=kk-i-l
                    jj=n-j+1
                    own=ruisekiL[i]+ruisekiR[j]
                    #print(i,j,own)
                    m=0
                    mcnt=0
                    while(mcnt<l):
                        if i<vv[m][0] and vv[m][0]<jj:
                            m+=1
                            continue
                        #print(i,jj,vv[m])
                        own-=vv[m][1]
                        mcnt+=1
                        m+=1
                    #print(own)
                    maxown=max(own,maxown)
    return maxown

if __name__=='__main__':
    n,k,v=readinput()
    ans=main(n,k,v)
    print(ans)