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=[int(a) for a in input().split()]
able=min(N,K)
ans=0

for a in range(able+1):
    for b in range(able+1-a):
        if b==0:
            have=V[:a]
        else:
            have=V[:a]+V[-b:]
        have.sort()
        val=sum(have)
        
        if a+b==able:
            pass
        else:
            for i in range(min(len(have),able-a-b)):
                if have[i] < 0:
                    val-=have[i]
                else:
                    break
        
        if ans<val:
            ans=val

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
V=[int(a) for a in input().split()]
able=min(N,K)
ans=0

for a in range(able+1):
    for b in range(able+1-a):
        if b==0:
            have=V[:a]
        else:
            have=V[:a]+V[-b:]
        have.sort()
        val=sum(have)
        
        if a+b==able:
            pass
        else:
            for i in range(min(len(have),able+1-a-b)):
                if have[i] < 0:
                    val-=have[i]
                else:
                    break
        
        if ans<val:
            ans=val

print(ans)

Submission status: Wrong Answer

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

ans = 0
for a in range(min(n, k) + 1):
    for b in range(min(n, k) - a):
        li = v[:a]
        if b > 0:
            li += v[-b:]
        li.sort()
        count = 0
        while (a + b + count < k):
            if len(li) > 0 and li[0] < 0:
                count += 1
                li = li[1:]
            else:
                break
        ans = max(ans, sum(li))

print(ans)

Submission status: Wrong Answer

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

ans = 0
for a in range(min(n, k)):
    for b in range(min(n - a, k - a)):
        li = v[:a]
        if b > 0:
            li += v[-b:]
        li.sort()
        count = 0
        while (a + b + count < k):
            if len(li) > 0 and li[0] < 0:
                count += 1
                li = li[1:]
            else:
                break
        ans = max(ans, sum(li))

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
v = [int(i) for i in input().split()]

# 左右から計min(n,k-i)個取り出す
# 0<=i<=k//2
# i個捨てる

ans = 0
# k_i回行う
for k_i in range(min(2*n,k+1)):
  # i個捨てる
  for i in range(k_i//2+1):
    #左からj個、右から残りを取る
    for j in range(k_i-i+1):
      #print(j)
      hand_tmp = v[:j] + v[::-1][:k_i-i-j]
      hand_tmp.sort()
      #print(hand_tmp)
      # 小さいものからi個捨てる
      hand_tmp = hand_tmp[i:]
      ans_tmp = sum(hand_tmp)
      if ans_tmp > ans:
        ans = ans_tmp
    
      
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
v = [int(i) for i in input().split()]

# 左右から計k-i個取り出す
# 0<=i<=k//2
# i個捨てる
# 手持ちはk-2*i個

ans = 0
for k_i in range(k+1):
  for i in range(k_i//2+1):
    # k-i個の選び方
    for j in range(k_i-i+1):
      #print(j)
      hand_tmp = v[:j] + v[::-1][:k_i-i-j]
      hand_tmp.sort()
      #print(hand_tmp)
      hand_tmp = hand_tmp[i:]
      ans_tmp = sum(hand_tmp)
      if ans_tmp > ans:
        ans = ans_tmp
    
      
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
v = [int(i) for i in input().split()]

# 左右から計k-i個取り出す
# 0<=i<=k//2
# i個捨てる
# 手持ちはk-2*i個

ans = 0
for k_i in range(k):
  for i in range(k_i//2+1):
    # k-i個の選び方
    for j in range(k_i-i+1):
      #print(j)
      hand_tmp = v[:j] + v[::-1][:k_i-i-j]
      hand_tmp.sort()
      #print(hand_tmp)
      hand_tmp = hand_tmp[i:]
      ans_tmp = sum(hand_tmp)
      if ans_tmp > ans:
        ans = ans_tmp
    
      
print(ans)

Submission status: Wrong Answer

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

max_len=min(N,K)

ans=0
for l in range(max_len):
	for r in range(max_len-l+1):
		T=V[:l]
		if r!=0:	T+=V[-1*r:]
		suteru=K-(l+r)
		T.sort()

		for i in range( min( suteru, len(T)) ):
			if T[i]<0:
				T[i]=0
		ans=max( ans, sum(T) )

print ans

Submission status: Wrong Answer

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

max_len=min(N,K)


ans=0
for l in range(max_len):
	for r in range(max_len-l):
		T=V[:l]
		if r!=0:	T+=V[-1*r:]
		suteru=K-(l+r)
		T.sort()

		for i in range( min( suteru, len(T)) ):
			if T[i]<0:
				T[i]=0
		ans=max( ans, sum(T) )

print ans

Submission status: Wrong Answer

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

for left in range(k + 1):
    for right in range(k - left + 1):
        if left + right > n:
            break
        hold = []
        hold.extend(v[:left])
        hold.extend(v[n-right:])
        hold.sort()
        total = sum(hold)
        if len(hold) >= k - left - right:
            for i in range(k - left - right):
                if hold[i] < 0:
                    total -= hold[i]
                if 0 <= hold[i]:
                    break
        M = max(M, total)

print(M)

Submission status: Wrong Answer

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

for left in range(k + 1):
    for right in range(k - left + 1):
        hold = []
        hold.extend(v[:left])
        hold.extend(v[n-right:])
        hold.sort()
        total = sum(hold)
        for i in range(k - left - right):
            if i >= len(hold):
                break
            if 0 <= hold[i]:
                break
            if hold[i] < 0:
                total -= hold[i]

        M = max(M, total)

print(M)

Submission status: Wrong Answer

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

for left in range(k + 1):
    for right in range(k - left + 1):
        hold = []
        hold.extend(v[:left])
        hold.extend(v[n-right:])
        hold.sort()
        total = sum(hold)
        if len(hold) - 1 >= k - left - right:
            for i in range(k - left - right):
                if hold[i] < 0:
                    total -= hold[i]
                if 0 <= hold[i]:
                    break
        M = max(M, total)

print(M)

Submission status: Wrong Answer

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

for left in range(k + 1):
    for right in range(k - left + 1):
        hold = []
        hold.extend(v[:left])
        hold.extend(v[n-right:])
        hold.sort()
        total = sum(hold)
        if len(hold) >= k - left - right:
            for i in range(k - left - right):
                if hold[i] < 0:
                    total -= hold[i]
                if 0 <= hold[i]:
                    break
        M = max(M, total)

print(M)

Submission status: Wrong Answer

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

for left in range(k + 1):
    for right in range(k - left + 1):
        hold = []
        hold.extend(v[:left])
        hold.extend(v[n-right:])
        hold.sort()
        total = sum(hold)
        if len(hold) >= k - left - right:
            for i in range(k - left - right):
                if hold[i] < 0:
                    total -= hold[i]
                if 0 <= hold[i]:
                    break
        print(hold)
        print(total)
        print(k - left - right)

        M = max(M, total)

print(M)

Submission status: Wrong Answer

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

for left in range(k + 1):
    for right in range(k - left + 1):
        hold = []
        hold.extend(v[:left])
        hold.extend(v[n-right:])
        hold.sort()
        total = sum(hold)
        if len(hold) > k - left - right:
            for i in range(k - left - right):
                if hold[i] < 0:
                    total -= hold[i]
                if 0 <= hold[i]:
                    break
            M = max(M, total)

print(M)

Submission status: Wrong Answer

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

for left in range(k + 1):
    for right in range(k - left + 1):
        hold = []
        hold.extend(v[:left])
        hold.extend(v[n-right:])
        hold.sort()
        total = sum(hold)
        if len(hold) > k - left - right:
            for i in range(k - left - right):
                if hold[i] < 0:
                    total -= hold[i]
                if 0 <= hold[i]:
                    break
        M = max(M, total)

print(M)

Submission status: Wrong Answer

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

for left in range(K + 1):
    for right in range(K - left + 1):
        hold = []
        if left != 0:
            hold.extend(v[:left])
        if right != 0:
            hold.extend(v[-right:])
        hold.sort()
        total = sum(hold)
        if len(hold) > k - left - right:
            for i in range(k - left - right):
                if hold[i] < 0:
                    total -= hold[i]
                if 0 < hold[i]:
                    break
        if M < total:
            M = total

print(M)

Submission status: Wrong Answer

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

for left in range(k + 1):
    for right in range(k - left + 1):
        hold = []
        if left != 0:
            hold.extend(v[:left])
        if right != 0:
            hold.extend(v[-right:])
        hold.sort()
        total = sum(hold)
        if len(hold) > k - left - right:
            for i in range(k - left - right):
                if hold[i] < 0:
                    total -= hold[i]
                if 0 < hold[i]:
                    break
        if M < total:
            M = total

print(M)

Submission status: Wrong Answer

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

v = list(map(int, input().split()))
M = 0

for left in range(k + 1):
    for right in range(k - left + 1):
        hold = []
        if left != 0:
            hold.extend(v[:left])
        if right != 0:
            hold.extend(v[-right:])
        hold.sort()
        total = sum(hold)
        if len(hold) > k - left - right:
            for i in range(k - left - right):
                if hold[i] < 0:
                    total -= hold[i]
                if 0 < hold[i]:
                    break
        if M < total:
            M = total

print(M)

Submission status: Wrong Answer

Solution code:
# -*- coding utf-8 -*-

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

VN = [0] + VN + [0]

score_a = 0
neg_l = []

ans = 0
for a in range(N):
    l = VN[a]
    score_a += l
    if l < 0:
        neg_l.append(l)

    score_b = 0
    neg_r = []
    for b in range(N):
        r = VN[N + 1 - b]
        score_b += r
        if r < 0:
            neg_r.append(r)

        if a + b > K or a + b > N:
            break
        else:
            negs = neg_l + neg_r
            negs.sort()
            score_cd = sum(negs[:min(len(negs), K - a - b)])
            score = score_a + score_b - score_cd
            ans = max(ans, score)
print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
n, k = map(int, input().split())
v = list(map(int, input().split()))
pick_max= min(n,k)
ans = 0
for i in range(pick_max + 1):
    for j in range(pick_max - i + 1):
        pick_left =v[:i]
        pick_right=v[-j:] if j > 0 else []
        pick_all =pick_left + pick_right
        pick_all.sort()
        k = pick_max - i - j
        dump = min(bisect_left(pick_all,0),k)
        ans =max(ans,sum(pick_all[dump:]))
print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right
n, k = map(int, input().split())
v = list(map(int, input().split()))
pick_max= min(n,k)
ans = 0
for i in range(pick_max + 1):
    for j in range(pick_max - i + 1):
        pick_left =v[:i]
        pick_right=v[-j:] if j > 0 else []
        pick_all =pick_left + pick_right
        pick_all.sort()
        k = pick_max - i - j
        dmp = min(bisect_right(pick_all,0),k)
        ans =max(ans,sum(pick_all[dmp:]))
print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right
n, k = map(int, input().split())
v = list(map(int, input().split()))
pick_max= min(n,k)
ans = 0
for i in range(pick_max + 1):
    for j in range(pick_max - i + 1):
        pick_left =v[:i]
        pick_right=v[-j:] if j > 0 else []
        pick_all =pick_left + pick_right
        pick_all.sort()
        k = pick_max - i - j
        dump = min(bisect_right(pick_all,0),k)
        ans =max(ans,sum(pick_all[dump:]))
print(ans)

Submission status: Wrong Answer

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

ans = 0
R = min(N, K)
for i in range(R + 1):
    for j in range(R - i + 1):
        A = []
        A += V[: i]
        if j > 0:
            A += V[- j :]
        A.sort()
        A2 = A[: R - (i + j)]
        for a in A2:
            if a < 0:
                A.remove(a)
        tmp = sum(A)
        ans = max(ans, tmp)
                
print(ans)

Submission status: Wrong Answer

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

# abc128_d

import collections
import itertools

def find_index(iterable, func):
    i = 0
    for it in iterable:
        if func(it):
            break
        i += 1
    return i

def slice_deq(deq, n):
    return itertools.islice(deq, 0, n)

def main():
    # Input
    n, k = [int(s) for s in input().split()]
    vs = (int(s) for s in input().split())
    # Get ans
    vs = collections.deque(vs)
    R = min(n, k)
    def gene_tv():
        for i in range(1, R+1):
            vs.rotate(-1)
            vs.reverse()
            for _ in range(i+1):
                temp = list(slice_deq(vs, i))
                print(temp)
                temp.sort()
                l = find_index(temp, lambda x: x>0)
                l = min(l, k-i)
                yield sum(temp[l:])
                vs.rotate(-1)
            vs.rotate(-(i-1))
    ans = max(gene_tv())
    ans = max(ans, 0)
    # Output
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

# abc128_d

import collections
import itertools

def find_index(iterable, func):
    i = 0
    for it in iterable:
        if func(it):
            break
        i += 1
    return i

def slice_deq(deq, n):
    return itertools.islice(deq, 0, n)

def main():
    # Input
    n, k = [int(s) for s in input().split()]
    vs = (int(s) for s in input().split())
    # Get ans
    vs = collections.deque(vs)
    R = min(n, k)
    def gene_tv():
        for i in range(1, R+1):
            vs.rotate(-1)
            vs.reverse()
            for _ in range(i+1):
                temp = list(slice_deq(vs, i))
                temp.sort()
                l = find_index(temp, lambda x: x>0)
                l = min(l, k-i)
                yield sum(temp[l:])
            vs.rotate(-(i-1))
    ans = max(gene_tv())
    ans = max(ans, 0)
    # Output
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

# abc128_d

def get_right(l, r):
    return l[-r:] if r!=0 else []

def main():
    # Input
    n, k = [int(s) for s in input().split()]
    vs = [int(s) for s in input().split()]
    # Get ans
    R = min(n, k)
    def gene_lr():
        for i in range(R):
            for j in range(i+1):
                yield(i-j, j)
    def gene_tv():
        for l, r in gene_lr():
            tv = 0
            pps = []
            ws = vs[:l] + vs[n-r:]
            for v in ws:
                tv += v
                if v<0:
                    pps.append(v)
            s = k-(l+r)
            pps.sort()
            yield tv-sum(pps[:s])
    ans = max(gene_tv())
    # Output
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

# abc128_d

def main():
    # Input
    n, k = [int(s) for s in input().split()]
    vs = [int(s) for s in input().split()]
    # Get ans
    k = min(k, n)
    ans = [0]
    def dfs(nc, tv, l, r, ms):
        m = sum(ms[:nc])
        ans[0] = max(ans[0], tv-m)
        #print(ans[0], nc, l, r, m)
        if nc <= 1: return
        n = vs[l+1-1]
        dfs(nc-1, tv+n, l+1, r, sorted(ms+[n]) if n<0 else ms)
        n = vs[r-1]
        dfs(nc-1, tv+n, l, r-1, sorted(ms+[n]) if n<0 else ms)
    dfs(k, 0, 0, 0, [])
    # Output
    print(ans[0])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
from itertools import product

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

max_v = -float("inf")
for actions in product(*([range(4)] * K)):
  d = deque(V)
  hand = []
  for action in actions:
    if action == 0:
      try:
        hand.append(d.popleft())
      except IndexError:
        break
        
    elif action == 1:
      try:
        hand.append(d.pop())
      except IndexError:
        break
        
    elif action == 2:
      if len(hand) > 0:
        min_hand = min(hand)
        if min_hand < 0:
          d.appendleft(min_hand)
          hand.remove(min_hand)
        
    elif action == 3:
      if len(hand) > 0:
        min_hand = min(hand)
        if min_hand < 0:
          d.append(min_hand)
          hand.remove(min_hand)

  sum_v = sum(hand)
  if max_v < sum_v:
    max_v = sum_v
    print(actions, max_v)
print(max_v)

Submission status: Wrong Answer

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

ans = 0
for i in range(1, min(N, K)+1):
    for j in range(1, i+1):
        take_list = []
        for k in range(j):
            take_list.append(V[k])
        for k in range(1, i-j+1):
            take_list.append(V[-k])
        take_list.sort()
        for l in range(min(N, K)-i):
            if len(take_list) >= 1:
                if take_list[0] < 0:
                    take_list.pop(0)
                else:
                    break
        ans = max(ans, sum(take_list))

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from heapq import heappush, heappop


def f(v, n):
    cnt_neg = 0
    for elem in reversed(v):
        if elem < 0:
            cnt_neg += 1
        else:
            break
    v = v[:-cnt_neg] if cnt_neg > 0 else v
    h = []
    s = sum(v)
    tmp = max(s, 0)
    r = []
    for i in range(len(v)):
        if v[i] < 0:
            heappush(h, v[i])
        else:
            r.append(v[i])
    r = list(reversed(r))
    for _ in range(cnt_neg):
        if len(h) > 0:
            elem = heappop(h)
            s -= elem
            tmp = max(tmp, s)
    #print(v, r, h, s, cnt_neg)
    i = 0
    while len(h) > 0 and i < len(r):
        elem = heappop(h)
        s -= elem
        s -= r[i]
        i += 1
        tmp = max(tmp, s)
    return tmp


def solve(N: int, K: int, V: "List[int]"):
    ans = 0
    pn = [V[i] >= 0 for i in range(N)]
    for i in range(K+1):
        vl = V[:i]
        vr = list(reversed(V[-(min(N, K)-i):])) if i != min(N, K) else []

        tmpl = f(vl, i)
        tmpr = f(vr, K-i)

        ans = max(ans, tmpl+tmpr)

    print(ans)

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    V = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, K, V)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from heapq import heappush, heappop


def f(v, n):
    cnt_neg = 0
    for elem in reversed(v):
        if elem < 0:
            cnt_neg += 1
        else:
            break
    v = v[:-cnt_neg] if cnt_neg > 0 else v
    h = []
    s = sum(v)
    tmp = max(s, 0)
    r = []
    for i in range(len(v)):
        if v[i] < 0:
            heappush(h, v[i])
        else:
            r.append(v[i])
    r = list(reversed(r))
    for _ in range(cnt_neg):
        if len(h) > 0:
            elem = heappop(h)
            s -= elem
            tmp = max(tmp, s)
    #print(v, r, h, s, cnt_neg)
    i = 0
    while len(h) > 0 and i < len(r):
        elem = heappop(h)
        s -= elem
        s -= r[i]
        i += 1
        tmp = max(tmp, s)
    return tmp


def solve(N: int, K: int, V: "List[int]"):
    ans = 0
    pn = [V[i] >= 0 for i in range(N)]
    for i in range(K+1):
        vl = V[:i]
        vr = list(reversed(V[-(K-i):])) if i != K else []

        tmpl = f(vl, i)
        tmpr = f(vr, K-i)

        ans = max(ans, tmpl+tmpr)

    print(ans)

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    V = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, K, V)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from heapq import heappush, heappop


def solve(N: int, K: int, V: "List[int]"):
    ans = 0
    pn = [V[i] >= 0 for i in range(N)]
    for i in range(K+1):
        vl = V[:i]
        vr = list(reversed(V[-(K-i):])) if i != K else []

        cnt = 0
        for elem in reversed(vl):
            if elem < 0:
                cnt += 1
            else:
                break
        vl = vl[:-cnt] if cnt != 0 else vl
        hn = []
        suml = sum(vl)
        tmpl = max(suml, 0)
        for jl in range(len(vl)):
            if not pn[jl]:
                heappush(hn, vl[jl])
        tmp_val_l = [0]*cnt
        for jl in reversed(range(len(vl))):
            if pn[jl]:
                tmp_val_l.append(vl[jl])
        jl = 0
        while len(hn) > 0 and jl < len(tmp_val_l):
            elem = heappop(hn)
            suml -= elem
            suml -= tmp_val_l[jl]
            jl += 1
            tmpl = max(tmpl, suml)
        if jl == len(tmp_val_l):
            while len(hn) > 0:
                elem = heappop(hn)
                suml -= elem
                tmpl = max(tmpl, suml)

        cnt = 0
        for elem in reversed(vr):
            if elem < 0:
                cnt += 1
            else:
                break
        vr = vr[:-cnt] if cnt != 0 else vr
        hn = []
        sumr = sum(vr)
        tmpr = max(sumr, 0)
        for jr in range(len(vr)):
            if not pn[N-jr-1]:
                heappush(hn, vr[jr])
        tmp_val_r = [0]*cnt
        for jr in reversed(range(len(vr))):
            if pn[N-jr-1]:
                tmp_val_r.append(vr[jr])
        jr = 0
        while len(hn) > 0 and jr < len(tmp_val_r):
            elem = heappop(hn)
            sumr -= elem
            sumr -= tmp_val_r[jr]
            jr += 1
            tmpr = max(tmpr, sumr)
        if jr == len(tmp_val_r):
            while len(hn) > 0:
                elem = heappop(hn)
                sumr -= elem
                tmpr = max(tmpr, sumr)

        ans = max(ans, tmpl+tmpr)
    print(ans)

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    V = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, K, V)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import heapq
from collections import deque

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

res = 0 # 答えの初期値
for i in range(K+1): # 左端から宝石を取り出す回数
    for j in range(K+1-i): # 右端から宝石を取り出す回数
        sub = K - i - j # 価値がマイナスの宝石を捨てる回数
        d = deque(v) # dequeに詰まった宝石
        a = [] # 持っている宝石
        
        # 左端から宝石を取り出す
        cnt = 0
        while (cnt < i) & (len(d) > 0):
            heapq.heappush(a, d.popleft())
            cnt += 1
            
        # 右端から宝石を取り出す
        cnt = 0
        while (cnt < j) & (len(d) > 0):
            heapq.heappush(a, d.pop())
            cnt += 1
        
        # 試行回数が許す限り手持ちの宝石から価値がマイナスのものを捨てる
        # マイナスでない事によるストップ→bを足す、そうでないなら足さない
        b = 0 # 捨てる宝石の価値
        cnt = 0
        while (cnt < sub) & (b >= 0) & (len(a) > 0):
            b = heapq.heappop(a)
            cnt += 1
        value = sum(a) + b if b > 0 else sum(a)
        res = max(res, value)
print(res)

Submission status: Wrong Answer

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

retn = 0

k_cap = k
if (k > n):
    k_cap = n

for l in range(k_cap):
    list_mine_l = list_v[:l]

    for r in range(k_cap-l):
        list_mine_r = list_mine_l.copy()
        if (r > 0):
            list_mine_r.extend(list_v[-r:])
        list_mine_r.sort()

        sss = len(list_mine_r)
        if (sss > k-l-r):
            sss = k-l-r
        for s in range(sss+1):
            list_mine_s = list_mine_r[s:]

            if (len(list_mine_s) > 0):
                tmp = sum(list_mine_s)
                if (retn < tmp):
                    retn = tmp

print(retn)

Submission status: Wrong Answer

Solution code:
import heapq
N, K = [int(_) for _ in input().split()]
V = [int(_) for _ in input().split()]
ans = 0
for il in range(min([N + 1, K + 1])):
    for ir in range(min([N + 1 - il, K + 1 - il])):
        H = V[:il] + V[N - ir:N]
        heapq.heapify(H)
        for d in range(il + ir):
            v = heapq.heappop(H)
            if v >= 0:
                now += v
                break
        now = sum(H)
        ans = max([ans, now])
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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

ans = 0
for pick in range(min(K+1, len(V))):
    for right in range(pick+1):
        left = pick-right
        tmp_list = V[:right]+V[N-left:]
        tmp_list = sorted(tmp_list)
        tmp_ans = sum(tmp_list)
        rem = min(K-pick, len(tmp_list))
        for i in range(rem):
            if tmp_list[i] < 0:
                tmp_ans -= tmp_list[i]
            else:
                break
        ans = max(tmp_ans, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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

ans = 0
for pick in range(K+1):
    for right in range(pick+1):
        left = pick-right
        tmp_list = V[:right]+V[N-left:]
        tmp_list = sorted(tmp_list)
        tmp_ans = sum(tmp_list)
        rem = min(K-pick, len(tmp_list))
        for i in range(rem):
            if tmp_list[i] < 0:
                tmp_ans -= tmp_list[i]
            else:
                break
        ans = max(tmp_ans, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n, k = f()
    v = f()

    ans = 0
    for take in range(min(k//2,k+1, n+1), min(k+1, n+1)):
        r_op = k - take
        for left in range(0, take+1):
            right = take - left
            nums = v[:left] + v[n- right:]
            nums = sorted(nums)
            cur_ans = sum(nums) 

            for i in range(r_op):
                if i<len(nums) and nums[i]<0:
                    cur_ans += abs(nums[i])

            ans = max(ans, cur_ans)
    
    print(ans)

            


solve()

Submission status: Wrong Answer

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n, k = f()
    v = f()

    ans = 0
    for take in range(k//2, min(k+1, n+1)):
        r_op = k - take
        for left in range(0, take+1):
            right = take - left
            nums = v[:left] + v[n- right:]
            nums = sorted(nums)
            cur_ans = sum(nums) 

            for i in range(r_op):
                if i<len(nums) and nums[i]<0:
                    cur_ans += abs(nums[i])

            ans = max(ans, cur_ans)
    
    print(ans)

            


solve()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
k = min(N,K)

vs = [int(v) for v in input().split()]

acts = []
for A in range(k):
    for B in range(k):
        if A+B>k:
            break
        acts.append((A,B))

ress = []
for A, B in acts:
    if A!=0:
        left = vs[:A]
    else:
        left = []
    if B!=0:
        right = vs[-B:]
    else:
        right =[]
    #print(A, B, left, right)
    join = left+right
    join = sorted(join)
    for l in range(K-A-B+1):
        res = sum(join[l:])
        ress.append(res)
print(max(ress))