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:

def get_data(data_left, nokori):
    # print(data_left, nokori)
    while 1:
        if len(data_left) == 0:
            break
        if data_left[-1] <= 0:
            data_left.pop(-1)
            nokori += 1
        else:
            break

    # print(data_left, nokori)

    while nokori > 0:
        if len(data_left) == 0:
            break
        min_data = min(data_left)
        if min_data < 0:
            data_left.pop(data_left.index(min_data))
            nokori -= 1
        else:
            break
        # print('uuu', data_left)

    # print(data_left)

    while 1:
        if len(data_left) == 0:
            break
        saigo = data_left[-1]
        min_data = min(data_left)

        if saigo + min_data < 0:
            data_left.pop(-1)

            if len(data_left) == 0:
                nokori += 1
                break
            if min_data not in data_left:
                nokori += 1
            else:
                # print(data_left.index(min_data))
                data_left.pop(data_left.index(min_data))
        else:
            break

    return nokori, data_left

def main():

    num_houseki, num_sousa = map(int, input().split())
    data = list(map(int, input().split()))


    ans = 0


    # dp = defaultdict(int)

    for i in range(num_sousa + 1):

        nokori = 0
        left_ind = min(i, num_houseki)
        nokori += i - left_ind
        righ_ind = max(left_ind, num_houseki - num_sousa + i)
        nokori += num_sousa - i - num_houseki + righ_ind
        data_left = data[:left_ind]
        data_right = data[righ_ind:]

        # print('<><<><<><')
        # print(data_left)
        # print(data_right)


        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]
        
        

        # print('----')
        # print(data_left)
        # print(data_right)

        kari = sum(data_right) + sum(data_left)
        if kari > ans:
            ans = kari

    print(ans)







if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if N >= K:
    a = K+1
else:
    a = N
for i in range(1,a)[::-1]:
    Vn = V[:i]
    Vns = Vn
    for k in range(K-i):
        if len(Vns)==0:
            Vns += [0]
            break 
        if min(Vns) < 0:
            Vns.pop(Vns.index(min(Vns)))
        else: break
    val = sum(Vns)
    if ans < val:   ans = val
    
    for j in range(1,i+1):
        Vn = [V[-j]] + Vn[:-1]
        Vns = Vn
        for k in range(K-i):
            if len(Vns)==0:
                Vns += [0]
                break 
            if min(Vns) < 0:
                Vns.pop(Vns.index(min(Vns)))
            else: break
        val = sum(Vns)
        if ans < val:   ans = val


print(max(0,ans))

Submission status: Wrong Answer

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

ans = 0
for l in range(K+1):
    for r in range(K+1-l):
        bag = V[:l] + V[N-r:] + [0]
        rest_ope = K-l-r
        for _ in range(rest_ope):
            min_bag = min(bag)
            if min_bag < 0 and rest_ope > 0:
                bag.remove(min_bag)
                rest_ope -= 1
        else:
            ans = max(ans, sum(bag))

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
Vi = [i+1 for i in range(len(V))]
V.extend(V)
Vi.extend(Vi)
ans = 0
for k in range(1, min(K+1, N+1)):
    for s in range(len(V)):
        part_Vi = Vi[s:s+k]
        if 1 not in part_Vi and N not in part_Vi:
            continue
        can_remove = K - k - 1
        part_V = V[s:s+k]
        part_V = sorted(part_V)
        plus_i = 0
        for v in part_V:
            if v < 0:
                plus_i += 1
                can_remove -= 1
            else:
                break
            if can_remove <= 0:
                break
        ans = max(ans, sum(part_V[plus_i:]))
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):
    tmp = []
    for j in range(i+1):
        choice = v[:j] + v[-(i-j):]
        if i != j:
            if choice not in tmp:
                tmp.append(choice)
        else:
            tmp.append(v[:j])

    for cho in tmp:
        cho.sort()
        cho.reverse()
        for l in range(k-i+1):
            ans = max(ans, sum(cho[:l]))
            print(cho[:l])

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
lis = list(map(int,input().split()))
ans = 0
now = 0
for i in range(k+1):
  for j in range(0,min(n,k-i+1)):
    l = k-(i+j)
    li = lis[:i]
    if j > 0:
      li += lis[-j:]
    li.sort()
    for h in range(l):
      try:
        if min(li) < 0:
          li.remove(min(li))
      except:
        break
    ans = max(ans,sum(li))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

fin = sys.stdin.readline
N, K = [int(elem) for elem in fin().split()]

v_list = [int(elem) for elem in fin().split()]

sum_list = []
for p in range(K+1):
    for left in range(K-p+1):
        for right in range(K-p-left+1):
            if (left + right) < p:
                continue
            each_v_list = v_list[:left] + list(deque(v_list, right))
            each_v_list = sorted(each_v_list)
            each_v_list = each_v_list[p:]
            each_sum = sum(each_v_list)
            sum_list.append(each_sum)
print(max(max(sum_list), 0))

Submission status: Wrong Answer

Solution code:
N, K = (int(x) for x in input().split())
V = [int(x) for x in input().split()]
ans = 0
for left in range(K+1):
    for right in range(K+1-left):
        back = K - left - right
        tmp_list = V[:left] + V[N-right:]
        tmp_list.sort()
        index = 0
        t = min(len(tmp_list), back)
        for i in range(t):
            if tmp_list[i] > 0:
                break
            index += 1
        tmp = sum(tmp_list[index:])
        if tmp > ans:
            ans = tmp

print(ans)

Submission status: Wrong Answer

Solution code:
# 0~kまでx個左から取り、0~k-xまで右からy個取り、最後に0~k-x-yまで取ったうちで小さいやつを捨てる
n, k = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
for x in range(k + 1):
    for y in range(k - x + 1):
        for z in range(k - y + 1):
            if x + y < z or x + y + z > k:
                continue
            arr_tmpx = []
            arr_tmpy = []
            if x > 0:
                arr_tmpx = arr[:x]
            if y > 0:
                arr_tmpy = arr[-y:]
            arr_tmpx.extend(arr_tmpy)
            m = sum(list(sorted(arr_tmpx))[:z])
            ans = max(ans, sum(arr_tmpx)-m)

print(ans)

Submission status: Wrong Answer

Solution code:
a,k=map(int,input().split())
L=list(map(int,input().split()))
ans = 0

for i in range(k+1):
  for j in range(k-i+1):
    S=[0]+L[:i]+L[a-j:]
    S.sort()
    
    x=0
    while x < k-(j+i):
      if S[x]>=0:
        break
      x+=1
      
    ans=max(ans, sum(S)-sum(S[:x]) )
    
print(ans)

Submission status: Wrong Answer

Solution code:

def get_data(data_left, nokori):
    # print(data_left, nokori)
    while 1:
        if len(data_left) == 0:
            break
        if data_left[-1] <= 0:
            data_left.pop(-1)
            nokori += 1
        else:
            break

    # print(data_left, nokori)

    while nokori > 0:
        if len(data_left) == 0:
            break
        min_data = min(data_left)
        if min_data < 0:
            data_left.pop(data_left.index(min_data))
            nokori -= 1
        else:
            break
        # print('uuu', data_left)

    # print(data_left)

    while 1:
        if len(data_left) == 0:
            break
        saigo = data_left[-1]
        min_data = min(data_left)

        if saigo + min_data < 0:
            data_left.pop(-1)

            if len(data_left) == 0:
                nokori += 1
                break
            if min_data not in data_left:
                nokori += 1
            else:
                # print(data_left.index(min_data))
                data_left.pop(data_left.index(min_data))
        else:
            break

    return nokori, data_left

def main():

    num_houseki, num_sousa = map(int, input().split())
    data = list(map(int, input().split()))


    ans = 0


    # dp = defaultdict(int)

    for i in range(num_sousa + 1):

        nokori = 0
        left_ind = min(i, num_houseki)
        nokori += i - left_ind
        righ_ind = max(left_ind, num_houseki - num_sousa + i)
        nokori += num_sousa - i - num_houseki + righ_ind
        data_left = data[:left_ind]
        data_right = data[righ_ind:]

        # print('<><<><<><')
        # print(data_left)
        # print(data_right)


        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        nokori, data_left = get_data(data_left, nokori)
        nokori, data_right = get_data(data_right[::-1], nokori)
        data_right = data_right[::-1]

        # print('----')
        # print(data_left)
        # print(data_right)

        kari = sum(data_right) + sum(data_left)
        if kari > ans:
            ans = kari

    print(ans)







if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
a,b = map(int,input().split())
A=list(map(int,input().split()))

output=0
#for tr in range(b//2+1):
for tr in range(b):
    if b-tr+1>a+1:
        aaa=a+1
    else:
        aaa=b-tr+1
    for g in range(aaa):
    #for g in range(b-tr+1)):
        #print(g)
        tmparr1=[]
        tmparr1.extend(A[:g])
        if g-b+tr<0:
            #tmparr1.extend(A[g-b+tr:])
            tmparr1.extend(A[g-aaa+1:])
        #print(tmparr1)
        tmparr1.sort()
        #print(tmparr1)
        #print(tmparr1[tr:])

        #trまで捨てれる
        if output<sum(tmparr1[tr:]):
            output=sum(tmparr1[tr:])

        for t in range(tr):
            if output<sum(tmparr1[t:]) :
                output=sum(tmparr1[t:])
            #print(output)
print(output)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v = list(map(int, input().split()))
cum = [0] * (n+1)

for i in range(n):
    cum[i+1] = cum[i] + v[i]

ans = 0
cnt = 0
for i in range(n):
    for j in range(n):
        if i > j:
            continue
        l = (j - i + 1)
        if n - l > k:
            continue
        a = v[:i] + v[j+1:]
        b = a[:]
        a.sort()
        tmp = cum[n] - (cum[j+1] - cum[i])
        tmp1 =tmp
        for x in range(k - (n-l)):
            if x >= len(a) or a[x] >= 0:
                break
            tmp -= a[x]
        ans = max([ans, tmp])
        # print('{}:{}:{}'.format(b, tmp1, tmp))
        cnt += 1
ans = max([ans, v[0], v[-1]])
if k >= 3 and n >= 2:
    ans = max([ans, v[1], v[-2]])
if k >= 4 and n >= 3:
    ans = max([ans, sum(v[1:3]), sum(v[-3:-1])])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def SI(): return input()
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')

def main():
    N, K = LI()
    V_li = LI()

    maximum = 0
    for take_count in range(min(K+1, N)):
        for take_left in range(take_count+1):
            # if take_left > N: break
            took = V_li[:take_left] + ([] if take_count == take_left else V_li[-(take_count - take_left):])
            took = sorted(took)
            for trash_count in range(K - take_count):
                if took and took[0] < 0:
                    took.pop(0)
                else:
                    break
            if not took:  # empty
                 took = [0]
            # print(took)
            maximum = max(maximum, sum(took))
    if N==1:
        maximum = max(0, V_li[0])
    if K == N:
        maximum = max(maximum, sum(V_li))

    print(maximum)


main()

Submission status: Wrong Answer

Solution code:
from collections import deque

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

v0list = []
vnlist = []
for v in vlist:
  if v < 0:
    v0list.append(0)
    vnlist.append(v)
  else:
    v0list.append(v)
    vnlist.append(0)

vlist_rev = list(reversed(vlist))
v0list_rev = list(reversed(v0list))
vnlist_rev = list(reversed(vnlist))
    
q = deque(vlist)
left_max_list = [0]*(K+1)
right_max_list = [0]*(K+1)

for i in range(1,K+1):
  max_value = -10**10
  for j in range(1,i+1):
    value = sum(vlist[0:j])-sum(vnlist[0:i-j])
    #print("{} {} {}".format(i,j,value))
    if max_value < value:
      max_value = value      
  left_max_list[i] = max_value
  
for i in range(1,K+1):
  max_value = -10**10
  for j in range(1,i+1):
    value = sum(vlist_rev[0:j])-sum(vnlist_rev[0:i-j])
    if max_value < value:
      max_value = value      
  right_max_list[i] = max_value

#print(left_max_list)
#print(right_max_list)
rrlist_rev = list(reversed(right_max_list))
max_value = -10**10
for i in range(K+1):
  value = left_max_list[i]+rrlist_rev[i]  
  if max_value < value:
    max_value = value
    
print(max_value)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

fin = sys.stdin.readline
N, K = [int(elem) for elem in fin().split()]

v_list = [int(elem) for elem in fin().split()]

sum_list = []
for p in range(K+1):
    for left in range(K-p+1):
        for right in range(K-p-left+1):
            if (left + right) < p:
                continue
            each_v_list = v_list[:left] + list(deque(v_list, right))
            each_v_list = sorted(each_v_list)
            each_v_list = each_v_list[p:]
            each_sum = sum(each_v_list)
            sum_list.append(each_sum)
print(max(sum_list))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for pull in range(k + 1):
    back = k - pull
    for i in range(pull + 1):
        for l in range(i + 1):
            r = i - l
            if r > 0:
                hand = V[:l] + V[-r:]
            else:
                hand = V[:l]
            hand = sorted(hand, reverse=True)
            for _ in range(back):
                if len(hand) == 0:
                    break
                if hand[-1] < 0:
                    hand.pop()
                else:
                    break
            ans = max(ans, sum(hand))
print(ans)

Submission status: Wrong Answer

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

left = np.cumsum([0] + v)
right = np.cumsum([0] + v[::-1])

results = []
for backn in range(k + 1):
    at = k - backn

    t = min(n, at)
    # print('----', backn, t)
    for shift in range(t + 1):
        p = v[:t - shift] + v[n - shift:]
        # print(v[:t - shift], v[n - shift:])
        p.sort()
        value = sum(p[backn:])
        results.append(value)
print(max(results + [0]))

Submission status: Wrong Answer

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

ans = 0
for l in range(K+1):
    for r in range(K+1-l):
        bag = V[:l] + V[N-r:] + [0]
        rest_ope = K-l-r
        for _ in range(rest_ope):
            min_bag = min(bag)
            if min_bag < 0 and rest_ope > 0:
                bag.remove(min_bag)
        else:
            ans = max(ans, sum(bag))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l():
	return list(map(int,input().split()))
def uy():
	return map(int,input().split())
def s(x):
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x):
	return " ".join(map(str,x))

n,m = uy()

a = l()

su = 0
for i in range(n):
	ll = max(i,n-(m - i))
	for j in range(ll,n+1):
		q = []
		co = 0
		p = a[:i]
		pp = a[j:]
		for k in range(len(p)):
			if p[k] < 0:
				q.append(p[k])
		for k in range(len(pp)):
			if pp[k] < 0:
				q.append(pp[k])
		co += sum(p)
		co += sum(pp)
		q.sort()
		for k in range(min(len(q),j-ll)):
			co += abs(q[k])
		su = max(su,co)
print(su)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

fin = sys.stdin.readline
N, K = [int(elem) for elem in fin().split()]

v_list = [int(elem) for elem in fin().split()]

sum_list = []
for p in range(K+1):
    for left in range(K-p+1):
        for right in range(K-p-left+1):
            if (left + right) < p:
                print(p, left, right, '| continue')
                continue
            each_v_list = v_list[:left] + list(deque(v_list, right))
            each_v_list = sorted(each_v_list)
            each_v_list = each_v_list[p:]
            each_sum = sum(each_v_list)
            print(p, left, right, '|', each_sum)
            sum_list.append(each_sum)
print(max(sum_list))

Submission status: Wrong Answer

Solution code:
a,b = map(int,input().split())
A=list(map(int,input().split()))

output=0
#for tr in range(b//2+1):
for tr in range(b):
    if b-tr+1>a+1:
        aaa=a+1
    else:
        aaa=b-tr+1
    for g in range(aaa):
    #for g in range(b-tr+1)):
        #print(g)
        tmparr1=[]
        tmparr1.extend(A[:g])
        if g-b+tr<0:
            #tmparr1.extend(A[g-b+tr:])
            tmparr1.extend(A[g-aaa-1:])
        #print(tmparr1)
        tmparr1.sort()
        #print(tmparr1)
        #print(tmparr1[tr:])

        #trまで捨てれる
        if output<sum(tmparr1[tr:]):
            output=sum(tmparr1[tr:])

        for t in range(tr):
            if output<sum(tmparr1[t:]) :
                output=sum(tmparr1[t:])
            #print(output)
print(output)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v = list(map(int, input().split()))
cum = [0] * (n+1)

for i in range(n):
    cum[i+1] = cum[i] + v[i]

ans = 0
cnt = 0
for i in range(n):
    for j in range(n):
        if i > j:
            continue
        l = (j - i + 1)
        if n - l > k:
            continue
        a = v[:i] + v[j+1:]
        b = a[:]
        a.sort()
        tmp = cum[n] - (cum[j+1] - cum[i])
        tmp1 =tmp
        for x in range(k - (n-l)):
            if x >= len(a) or a[x] >= 0:
                break
            tmp -= a[x]
        ans = max([ans, tmp])
        print('{}:{}:{}'.format(b, tmp1, tmp))
        cnt += 1
ans = max([ans, v[0], v[-1]])
if k >= 3 and n >= 2:
    ans = max([ans, v[1], v[-2]])
if k >= 4 and n >= 3:
    ans = max([ans, sum(v[1:3]), sum(v[-3:-1])])
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
ans=0
KK=K
if K>N:
    KK=N
for i in range(KK+1):
    j=KK-i
    a=V[:i]
    b=V[N-j:]
    ans=max(ans,sum(a)+sum(b))
    k=sum(a)+sum(b)
    c=a+b
    c.sort(reverse=True)
    W=K
    W-=2
    ab=[0,0]
    while(W>0):
        if c==[]:
            break
        t=c.pop()
        if ab[0]==0 and a!=[] and t==a[-1]:
            a.pop(-1)
            W+=1
            k=k-t
            ans=max(k,ans)
            continue
        elif ab[1]==1 and b!=[] and t==b[0]:
            b.pop(0)
            W+=1
            k=k-t-h
            ans=max(k,ans)
            continue
        elif t in a:
            a.remove(t)
            ab[0]=1
        elif t in b:
            b.remove(t)
            ab[1]=1
        else:
            print("W")
            quit()
        if a!=[] and b!=[] :
            if a[-1]<=b[0]:
                h=a.pop(-1)
            else:
                h=b.pop(0)
        elif a!=[] and b==[]:
            h=a.pop(-1)
        elif b!=[] and a==[]:
            h=b.pop(0)
        else:
            break
        c.remove(h)
        k=k-t-h
        ans=max(k,ans)
        W-=2
print(ans)

Submission status: Wrong Answer

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

max_score = 0
max_l = min(k, n)
for l in range(max_l + 1):
    l_score = -10000000
    cand = []
    for i in range(l):
        cand.append(v_list[i])
    for i in range(l // 2 + 1):
        eraberu = l - i * 2
        cand2 = cand[:l-i]
        cand2.sort(reverse=True)
        s = sum(cand2[:eraberu])
        if s > l_score:
            l_score = s

    r = max_l - l
    r_score = -10000000
    cand = []
    for i in range(r):
        cand.append(v_list[len(v_list) - r + i])
    for i in range(r // 2 + 1):
        eraberu = r - i * 2
        cand2 = cand[i:]
        cand2.sort(reverse=True)
        s = sum(cand2[:eraberu])
        if s > r_score:
            r_score = s

    score = l_score + r_score
    if score > max_score:
        max_score = score
print(max_score)

Submission status: Wrong Answer

Solution code:
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def SI(): return input()
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')

def main():
    N, K = LI()
    V_li = LI()

    maximum = 0
    for take_count in range(min(K+1, N)):
        for take_left in range(take_count+1):
            # if take_left > N: break
            took = V_li[:take_left] + ([] if take_count == take_left else V_li[-(take_count - take_left):])
            took = sorted(took)
            for trash_count in range(K - take_count):
                if took and took[0] < 0:
                    took.pop(0)
                else:
                    break
            if not took:  # empty
                 took = [0]
            # print(took)
            maximum = max(maximum, sum(took))
    if N==1:
        maximum = max(0, V_li[0])
    if K == N:
        if min(V_li) >= 0:
            maximum = sum(V_li)

    print(maximum)


main()

Submission status: Wrong Answer

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

def region(V, k):
    l = len(V)
    return [V[0:i] + V[l-k+i:] for i in range(k+1)]

def choice(region, K):
    te = len(region)
#    print("-----------")
#    print("te = ", te)
#    print(region)
#    print(sorted(region)[K-te:])
#    print("-----------")
    skip = K-te
    return sum(sorted(region)[skip:])

k_region = min(N, K)
print(max([choice(r, K) for k in range(1, k_region+1) for r in region(V, k)]))

Submission status: Wrong Answer

Solution code:
N, K = [int(x) for x in input().split(" ")]
D = [int(x) for x in input().split(" ")]
i = 0
j = len(D) - 1
memo = dict()

hands = set()

steps = 0

def maxSum(d, hands, k):
    if k == 0:
        return sum(hands)

    key = "{}::{}::{}".format(str(d), str(hands), k)
    if key in memo:
        return memo[key]
    
    patterns = [sum(hands)]

    if len(d) > 0:
        el = d[0]
        patterns.append(maxSum(d[1:], hands.union(set([el])), k - 1))
        if len(d) > 1:
            el = d[-1]
            patterns.append(maxSum(d[:-1], hands.union(set([el])), k - 1))

    if hands:
        m = min(hands)
        newHand = hands.difference(set([m]))
        patterns.append(maxSum([m] + d, newHand, k - 1))
        patterns.append(maxSum(d + [m], newHand, k - 1))

    res = max(patterns)
    memo[key] = res
    # global steps
    # steps += 1
    # print("HELLO", steps)
    return res

print(maxSum(D, set(), K))

Submission status: Wrong Answer

Solution code:
# まず取り出しきっちゃってから捨てる
# 左右からいくつずつ取り出すか

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

max_value = 0 # 何もとらない

for L in range(N):
  for R in range(N):
    if L+R > K:
      break
    if L+R > N:
      break
    get = []
    for i in range(L):
      get.append(V[i])
    for i in range(R):
      get.append(V[N-1-i])
    get.sort()
    rest = K - (L+R)
    minus = 0
    for x in get:
      if x < 0:
        minus += 1
    if minus > rest:
      value = sum(get[rest:])
    else:
      value = sum((x for x in get if x > 0))
    if value > max_value:
      max_value = value
    #print(L,R,rest,get,value)
      
      
   
print(max_value)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline

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

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



max_total = -10 ** 10

for npop in range(0, min(N, K) + 1):
    push_start = 0 if (K - npop) % 2 == 0 else 1
    
    for npush in range(push_start, min(K - npop, npop) + 1, 1):
        for n_left_pop in range(npop + 1): # N
            n_right_pop = npop - n_left_pop

            taken = V[:n_left_pop] + ([] if n_right_pop == 0 else V[-n_right_pop:])
            taken.sort()

            _total = sum(taken[npush:])
            max_total = max(max_total, _total)

print(max_total)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
import copy
originalV = copy.deepcopy(v)
gyakuv = v[::-1]
ans = 0
for i in range(k + 1): #i個使う
    if i >= n:
        break
    this = []
    for j in range(i + 1): #右からj個使う左からi - j // n > iの時
        #print(i)
        this = v[:j] + gyakuv[:min(max(i - j, 0), n - j)]
        #print(this, j, i)
        tmp = sum(this)
        this.sort()
        for asd in range(min(k - i, len(this))):
            if this[asd] >= 0:
                break
            tmp -= this[asd]

        ans = max(tmp, ans)


print(ans)

Submission status: Wrong Answer

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

left = np.cumsum([0] + v)
right = np.cumsum([0] + v[::-1])

results = []
for backn in range(k + 1):
    at = k - backn

    t = min(n, at)
    # print('----', backn, t)
    for shift in range(t + 1):
        p = v[:t - shift] + v[n - shift:]
        # print(v[:t - shift], v[n - shift:])
        p.sort()
        value = sum(p[backn:])
        results.append(value)
print(max(results))

Submission status: Wrong Answer

Solution code:
import heapq


def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    if N <= K:
        V.sort()
        total = sum(V)
        ans = max(total, 0)
        for i in range(min(K-N, N)):
            total -= V[i]
            ans = max(ans, total)
        print(ans)
    else:
        left = [0] * (K+1)
        right = [0] * (K+1)
        for i in range(K, 0, -1):
            lv = V[:i]
            rv = V[-i:]
            lsum = sum(lv)
            rsum = sum(rv)
            left[i] = lsum
            right[i] = rsum
            lheap = list(lv[:-1])
            rheap = list(rv[1:])
            heapq.heapify(lheap)
            heapq.heapify(rheap)
            for j in range(1, i):
                if i+j > K:
                    continue
                lm = heapq.heappop(lheap)
                left[i+j] = max(left[i+j], left[i+j-1] - lm)
                rm = heapq.heappop(rheap)
                right[i+j] = max(right[i+j], right[i+j-1] - rm)

        ans = 0
        for i in range(K+1):
            ans = max(max(left[i], 0) + max(right[K-i], 0), ans)
        print(ans)


if __name__ == "__main__":
    main()