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())
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)
    Amari = K-A-B
    trim_num = min(len(list(filter(lambda x:x<0, join))), Amari)
    #print(trim_num, join[trim_num:])
    res = sum(join[trim_num:])
    ress.append(res)
print(max(ress))

Submission status: Wrong Answer

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

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)
    Amari = K-A-B
    trim_num = min(len(list(filter(lambda x:x<0, join))), Amari)
    #print(trim_num, join[trim_num:])
    res = sum(join[trim_num:])
    ress.append(res)
print(max(ress))

Submission status: Wrong Answer

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

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)
    Amari = K-A-B
    trim_num = min(len(list(filter(lambda x:x>=0, join))), Amari)
    #print(trim_num, join[trim_num:])
    res = sum(join[trim_num:])
    ress.append(res)
print(max(ress))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
D = list(map(int, input().split()))
memo = []
queue = [[D, [], 0, 0]]
max = 0
while queue != []:
	now = queue.pop(0)
	if max < now[2]: max = now[2]
	if now[0] not in memo:
		memo.append(now[0])
		if now[3] < K:
			if now[0] != []:
				newD1 = []
				newD2 = []
				for i in range(0, len(now[0])):
					newD1.append(now[0][i])
					newD2.append(now[0][i])
				x = newD1.pop(0)
				if x < 0:
					newnegative = []
					for i in range(0, len(now[1])):
						newnegative.append(now[1][i])
					newnegative.append(x)
					newnegative.sort()
					queue.append([newD1, newnegative, now[2]+x, now[3]+1])
				else:
					queue.append([newD1, now[1], now[2]+x, now[3]+1])
				y = newD2.pop(-1)
				if y < 0:
					newnegative = []
					for i in range(0, len(now[1])):
						newnegative.append(now[1][i])
					newnegative.append(y)
					newnegative.sort()
					queue.append([newD1, newnegative, now[2]+y, now[3]+1])
				else:
					queue.append([newD1, now[1], now[2]+y, now[3]+1])
			if now[1] != []:
				newnegative = []
				for i in range(0, len(now[1])):
					newnegative.append(now[1][i])
				x = newnegative.pop(0)
				newD1 = []
				newD2 = []
				for i in range(0, len(now[0])):
					newD1.append(now[0][i])
					newD2.append(now[0][i])
				newD1.append(x)
				queue.append([newD1, newnegative, now[2]-x, now[3]+1])
				newD2.reverse()
				newD2.append(x)
				newD2.reverse()
				queue.append([newD2, newnegative, now[2]-x, now[3]+1])
print(max)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
v = 0
for i in range(K + 1):
    for j in range(K - i + 1):
        v_tmp = V[:i] + V[(N - j):]
        v_tmp.sort()
        for l in range(K - (i + j) + 1):
            v = max(v, sum(v_tmp[l:]))
print(v)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
kari = min(N,K)
v = 0
for i in range(kari  + 1):  # 取る1
    for j in range(kari  - i + 1):  # 取る2
        v_tmp = V[:i] + V[(N - j):]
        v_tmp.sort()
        for l in range(kari  - (i + j) + 1):  # 捨てる
            v = max(v, sum(v_tmp[l:]))
print(v)

Submission status: Wrong Answer

Solution code:
# D
N, K = map(int, input().split())
V = list(map(int, input().split()))
v = 0
for i in range(K + 1):  # 取る1
    for j in range(K - i + 1):  # 取る2
        v_tmp = V[:i] + V[(N - j):]
        v_tmp.sort()
        for l in range(K - (i + j) + 1):  # 捨てる
            v = max(v, sum(v_tmp[l:]))
print(v)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
v = 0
for i in range(K + 1):
    j = K - i
    for k in range(i + 1):
        v_tmp1 = V[:k] + V[(N - i + k):]
        v_tmp1.sort()
        for l in range(K - i + 1):
            v = max(v, sum(v_tmp1[l:]))
print(v)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
v = 0
for i in range(K+1):
    j = K - i
    for k in range(i+1):
        if (k == 0) and (i != 0):
            v_tmp1 = sum(V[-i:])
            v_tmp2 = [e for e in V[-i:] if e < 0]
            v_tmp2.sort()
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
            t = 1
        elif k == i:
            v_tmp1 = sum(V[:i])
            v_tmp2 = [e for e in V[:i] if e < 0]
            v_tmp2.sort()
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
            t = 2
        else:
            v_tmp1 = sum(V[:k] + V[(N - i + k):])
            v_tmp2 = [e for e in (V[:k] + V[(N - i + k):]) if e < 0]
            v_tmp2.sort()
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
            t = 3
        v = max(v, v_tmp)
print(v)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
v = 0
for i in range(K+1):
    j = K - i
    for k in range(i+1):
        if (k == 0) and (i != 0):
            v_tmp1 = sum(V[-i:])
            v_tmp2 = [e for e in V[-i:] if e < 0]
            v_tmp2.sort()
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
            t = 1
        elif k == i:
            v_tmp1 = sum(V[:i])
            v_tmp2 = [e for e in V[:i] if e < 0]
            v_tmp2.sort()
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
            t = 2
        else:
            v_tmp1 = sum(V[:k] + V[(N - k):])
            v_tmp2 = [e for e in (V[:k] + V[(N - k):]) if e < 0]
            v_tmp2.sort()
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
            t = 3
        v = max(v, v_tmp)
print(v)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
v = 0
for i in range(K+1):
    j = K - i
    for k in range(i+1):
        if (k == 0) and (i != 0):
            v_tmp1 = sum(V[-i:])
            v_tmp2 = [e for e in V[-i:] if e < 0]
            v_tmp2.sort()
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
        elif k == i:
            v_tmp1 = sum(V[:i])
            v_tmp2 = [e for e in V[:i] if e < 0]
            v_tmp2.sort()
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
        else:
            v_tmp1 = sum(V[:k] + V[-(i - k):])
            v_tmp2 = [e for e in (V[:k] + V[-(i - k):]) if e < 0]
            v_tmp2.sort()
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
        v = max(v, v_tmp)
print(v)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
if N * 2 <= K:
    ans = sum([e for e in V if e >= 0])
v = 0
for i in range(K+1):
    j = K - i
    for k in range(i+1):
        if (k == 0) and (i != 0):
            v_tmp1 = sum(V[-i:])
            v_tmp2 = [e for e in V[-i:] if e < 0]
            v_tmp2.sort()
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
        elif k == i:
            v_tmp1 = sum(V[:i])
            v_tmp2 = [e for e in V[:i] if e < 0]
            v_tmp2.sort()
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
        else:
            v_tmp1 = sum(V[:k] + V[-(i - k):])
            v_tmp2 = [e for e in (V[:k] + V[-(i - k):]) if e < 0]
            v_tmp2.sort()
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
        v = max(v, v_tmp)
print(v)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
if N <= K:
    ans = sum([e for e in V if e >= 0])
v = 0
for i in range(K+1):
    j = K - i
    for k in range(i+1):
        if (k == 0) and (i != 0):
            v_tmp1 = sum(V[-i:])
            v_tmp2 = [e for e in V[-i:] if e < 0]
            v_tmp2.sort()
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
        elif k == i:
            v_tmp1 = sum(V[:i])
            v_tmp2 = [e for e in V[:i] if e < 0]
            v_tmp2.sort()
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
        else:
            v_tmp1 = sum(V[:k] + V[-(i - k):])
            v_tmp2 = [e for e in (V[:k] + V[-(i - k):]) if e < 0]
            v_tmp2.sort()
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
        v = max(v, v_tmp)
print(v)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
if N <= K:
    ans = sum([e for e in V if e >= 0])
v = 0
for i in range(K+1):
    j = K - i
    for k in range(i+1):
        if (k == 0) and (i != 0):
            v_tmp1 = sum(V[-i:])
            v_tmp2 = [e for e in V[-i:] if e < 0]
            v_tmp2.sort(reverse=True)
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
        elif k == i:
            v_tmp1 = sum(V[:i])
            v_tmp2 = [e for e in V[:i] if e < 0]
            v_tmp2.sort(reverse=True)
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
        else:
            v_tmp1 = sum(V[:k] + V[-(i - k):])
            v_tmp2 = [e for e in (V[:k] + V[-(i - k):]) if e < 0]
            v_tmp2.sort(reverse=True)
            v_tmp = v_tmp1 - (sum(v_tmp2[:j]) if v_tmp2 else 0)
        v = max(v, v_tmp)
print(v)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
v = [int(i) for i in input().split()]
jewel = []
ans = 0
for i in range(min(k,n)):
  for j in range(min(k-i,n-i)):
    jewel = v[:i] + v[:-(j+1):-1]
    jewel.sort()
    temp = sum(jewel)
    leng = len(jewel)
    for m in range(k-i-j):
      if leng > m and jewel[m] < 0:
        temp += -jewel[m]
    ans = max(ans,temp)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
v = [int(i) for i in input().split()]
jewel = []
ans = 0
for i in range(k):
  for j in range(k-i):
    jewel = v[:i] + v[:-(j+1):-1]
    jewel.sort()
    temp = sum(jewel)
    leng = len(jewel)
    for m in range(k-i-j):
      if leng > m and jewel[m] < 0:
        temp += -jewel[m]
    ans = max(ans,temp)
print(ans)

Submission status: Wrong Answer

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


def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    reverse_v = v[::-1]
    ans = 0

    for left in range(min(n, k) + 1):
        for right in range(min(n, k) + 1):
            remain = min(n, k) - (left + right)

            if remain < 0:
                continue

            merged_v = sorted(v[:left] + reverse_v[:right])
            merged_v_len = len(merged_v)
            minus = 0

            if remain == 0 or merged_v_len == 0:
                continue

            for i in range(min(remain, merged_v_len)):
                if merged_v[i] < 0:
                    minus += merged_v[i]

            ans = max(ans, sum(merged_v) - minus)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    reverse_v = v[::-1]
    ans = 0

    for left in range(min(n, k) + 1):
        for right in range(min(n, k) + 1):
            remain = k - (left + right)

            if remain < 0:
                continue

            merged_v = sorted(v[:left] + reverse_v[:right])
            merged_v_len = len(merged_v)
            minus = 0

            for i in range(min(remain, merged_v_len)):
                if merged_v[i] < 0:
                    minus += merged_v[i]

            ans = max(ans, sum(merged_v) - minus)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    reverse_v = v[::-1]
    ans = 0

    for left in range(min(n, k) + 1):
        for right in range(min(n, k) + 1):
            remain = min(n, k) - (left + right)

            if remain < 0:
                continue

            merged_v = sorted(v[:left] + reverse_v[:right])
            merged_v_len = len(merged_v)
            minus = 0

            for i in range(min(remain, merged_v_len)):
                if merged_v[i] < 0:
                    minus += merged_v[i]

            ans = max(ans, sum(merged_v) - minus)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    reverse_v = v[::-1]
    ans = 0

    for left in range(k + 1):
        for right in range(k + 1):
            remain = k - (left + right)

            if remain < 0:
                continue

            merged_v = sorted(v[:left] + reverse_v[:right])
            merged_v_len = len(merged_v)
            minus = 0

            for i in range(min(remain, merged_v_len)):
                if merged_v[i] < 0:
                    minus += merged_v[i]

            ans = max(ans, sum(merged_v) - minus)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    reverse_v = v[::-1]
    ans = 0

    for left in range(k + 1):
        for right in range(k + 1):
            remain = k - (left + right)

            if remain < 0:
                break

            merged_v = sorted(v[:left] + reverse_v[:right])
            merged_v_len = len(merged_v)
            minus = 0

            for i in range(min(remain, merged_v_len)):
                if merged_v[i] < 0:
                    minus += merged_v[i]

            ans = max(ans, sum(merged_v) - minus)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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


Max = 0
for r in range(K + 1):
    for l in range(K - r + 1):
        rev = K - r - l
        having = None
        if r == 0:
            having = V_lst[:l]
        else:
            having = V_lst[:l] + V_lst[-r:]
        having.sort()
        for n in range(rev):
            if len(having) == 0:
                break
            if having[0] < 0:
                having.pop(0)
            else:
                break
        if Max < sum(having):
            Max = sum(having)
#        print(r,l,rev,Max)

print(Max)

Submission status: Wrong Answer

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

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


Max = 0
for r in range(K + 1):
    for l in range(K - r + 1):
        rev = K - r - l
        having = None
        if r == 0:
            having = V_lst[:l]
        else:
            having = V_lst[:l] + V_lst[-r:]
        having.sort()
        for n in range(rev):
            if having == []:
                break
            if having[0] < 0:
                having.pop(0)
            else:
                break
        if Max < sum(having):
            Max = sum(having)
        

print(Max)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
V_list = list(map(int,input().split()))
min_ = min(N,K)
total = [0]
#get
for a in range(min_+1):
  for b in range(min_-a+1):
    score_get = V_list[:a]+V_list[N-b:]
    score_get.sort()
    score = sum(score_get)
    for i in range(min(K-(a+b),len(score_get))):
      if score_get[i]<0:
        score -= score_get[i]
      else:
        break
      total.append(score)
print(max(total))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
V_list = list(map(int,input().split()))
min_ = min(N,K)
total = [0]
#get
for a in range(min_):
  for b in range(min_-a):
    score_get = V_list[:a]+V_list[N-b:]
    score_get.sort()
    if len(score_get) == 0:
      score_get.append(0)
    for i in range(min(K-(a+b)+1,len(score_get))):
      if score_get[i]<0:
        new_score = score_get[:i]+score_get[i+1:]
      else:
        break
      total_score = sum(new_score)
      total.append(total_score)
print(max(total))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
ans = 0
R = min(N,K)
for A in range(R+1):
    for B in range(R-A+1):
        s = V[:A]+V[N-B:]
        print(s)
        s.sort()
        S = sum(s)
        for k in range(min(K-A-B,len(s))):
            if s[k] < 0:
                S -= s[k]
        ans = max(ans,S)
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(K+1):
    for B in range(K-A+1):
        if A > N-1 or B > N-1:
            continue
        s = V[:A]+V[N-B:]
        #print(s)
        s.sort()
        S = sum(s)
        for k in range(min(K-A-B,len(s))):
            if s[k] < 0:
                S -= s[k]
        ans = max(ans,S)
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(K+1):
    for B in range(K-A+1):
        if A == B == 0:
            continue
        if A > N-1 or B > N-1:
            continue
        s = V[:A]+V[N-B:]
        
        s.sort()
        S = sum(s)
        for k in range(min(K-A-B,len(s))):
            if s[k] < 0:
                S -= s[k]
        ans = max(ans,S)
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(K):
    for B in range(K-A):
        if A == B == 0:
            continue
        if A > N or B > N:
            continue
        s = V[:A]+V[N-B:]
        s.sort()
        S = sum(s)
        for k in range(min(K-A-B,len(s))):
            if s[k] < 0:
                S -= s[k]
        ans = max(ans,S)
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(K):
    for B in range(K-A):
        if A == B == 0:
            continue
        if A > N-1 or B > N-1:
            continue
        s = V[:A]+V[N-B:]
        s.sort()
        S = sum(s)
        for k in range(min(K-A-B,len(s))):
            if s[k] < 0:
                S -= s[k]
        ans = max(ans,S)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0 
# 左からi + 1個取る場合
for i in range(min(n, k) + 1):
    # 右からi + 1個取る
    for j in range(min(n, k) - i + 1):
        par_seq = v[:i] + v[-j:] if j != 0 else v[:i]
        if sum(par_seq) > ans:
            ans = sum(par_seq)
        ope = k - (i + j)
        for i in range(ope):
            if not par_seq:
                continue
            minv = par_seq.pop(par_seq.index(min(par_seq)))
            if minv < 0 and sum(par_seq) > ans:
                ans = sum(par_seq) 
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = [int(i) for i in input().split()]
res = []
for l in range(k+1):
    tmp1 = v[:l]
    res.append(sum(tmp1))
    for r in range(k-l):
        if r == 0:
            tmp2 = tmp1
        else:
            tmp2 = tmp1 + v[-r:]
        res.append(sum(tmp2))
        for a in range(k-l-r):
            tmp2.sort()
            tmp3 = tmp2[a+1:]
            res.append(sum(tmp3))
print(max(res))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = [int(i) for i in input().split()]
res = []
for l in range(k+1):
    tmp1 = sorted(v[:l])
    res.append(sum(tmp1))
    for r in range(k-l):
        if r == 0:
            tmp2 = tmp1
        else:
            tmp2 = sorted(tmp1 + v[-r:])
        res.append(sum(tmp2))
        for a in range(k-l-r):
            tmp3 = tmp2[a+1:]
            res.append(sum(tmp3))
print(max(res))

Submission status: Wrong Answer

Solution code:
import sys
#import numpy as np

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N, K = i2nn()
    V = i2nn()
    n = 0
    for l in range(K):
        for r in range(K-l):
            if l + r > N:
                continue
            v = V[:l]
            if r > 0:
                v += V[-r:]
            vp = []
            vn = []
            for t in v:
                if t > 0:
                    vp.append(t)
                elif t < 0:
                    vn.append(t)
            vn.sort()
            k = K-l-r
            n2 = sum(vn[k:]) + sum(vp)
            if n < n2:
                n = n2
    print(n)

main()

Submission status: Wrong Answer

Solution code:

def solve2():
    N, K = [int(_) for _ in raw_input().split()]
    V = [int(_) for _ in raw_input().split()]
    MAX_GET = min(N, K)
    ans = 0
    for i in range(MAX_GET + 1):
        for j in range(MAX_GET + 1 - i):
            get = V[:i] + V[(N - j):]
            if len(get) == 0:
                continue
            get.sort(reverse = True)
            for k in range(MAX_GET - i - j):
                if len(get) > 0 and get[-1] < 0:
                    get.pop()
                else:
                    break
            ans = max(ans, sum(get))
    print ans

if __name__ == '__main__':
    solve2()

Submission status: Wrong Answer

Solution code:

def solve2():
    N, K = [int(_) for _ in raw_input().split()]
    V = [int(_) for _ in raw_input().split()]
    MAX_GET = min(N, K)
    ans = 0
    for i in range(MAX_GET + 1):
        for j in range(MAX_GET + 1 - i):
            get = V[:i] + V[(N - j):]
            if len(get) == 0:
                break
            get.sort(reverse = True)
            for k in range(MAX_GET - i - j):
                if len(get) > 0 and get[-1] < 0:
                    get.pop()
                else:
                    break
            ans = max(ans, sum(get))
    print ans

if __name__ == '__main__':
    solve2()

Submission status: Wrong Answer

Solution code:

def solve2():
    N, K = [int(_) for _ in raw_input().split()]
    V = [int(_) for _ in raw_input().split()]
    MAX_GET = min(N, K)
    ans = 0
    for i in range(MAX_GET):
        for j in range(MAX_GET - i):
            get = V[:i] + V[(N - j):]
            if len(get) == 0:
                break
            get.sort(reverse = True)
            for k in range(MAX_GET - i - j):
                if len(get) > 0 and get[-1] < 0:
                    get.pop()
                else:
                    break
            ans = max(ans, sum(get))
    print ans

if __name__ == '__main__':
    solve2()

Submission status: Wrong Answer

Solution code:
import sys
#import numpy as np

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

from collections import deque

def main():
    N, K = i2nn()
    V = i2nn()
    """
    p1 = [0] * (N+1)
    p2 = [0] * (N+1)
    n1 = [0] * (N+1)
    n2 = [0] * (N+1)
    for i in range(N):
        t = V[i]
        p = t if t > 0 else 0
        n = t if t < 0 else 0
        p1[i+1] = p1[i] + p
        n1[i+1] = n1[i] + n
    for i in range(N, -1, -1):
        t = V[i]
        p = t if t > 0 else 0
        n = t if t < 0 else 0
        p2[i-1] = p2[i] + p
        n2[i-1] = n2[i] + n
    """
    n = 0
    for l in range(K):
        for r in range(K-l):
            v = V[:l]
            if r > 0:
                v += V[-r:]
            vp = []
            vn = []
            for t in v:
                if t > 0:
                    vp.append(t)
                elif t < 0:
                    vn.append(t)
            vn.sort()
            k = K-l-r
            n2 = sum(vn[k:]) + sum(vp)
            if n < n2:
                n = n2
    print(n)

main()

Submission status: Wrong Answer

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

from heapq import heappush, heappop
for l in range(min(n, k)):
  for r in range(min(n-l, k-l)):
    minus = []
    cur = 0
    for ll in range(l):
      cur += v[ll]
      if v[ll]<0:
        heappush(minus, v[ll])
    for rr in range(r):
      cur += v[n-rr-1]
      if v[n-rr-1]<0:
        heappush(minus, v[n-rr-1])
    rm = k-l-r
    while rm>0 and len(minus)>0:
      rm -= 1
      cur -= heappop(minus)
    ans = max(ans, cur)
print(ans)

Submission status: Wrong Answer

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

from heapq import heappush, heappop
for l in range(min(n, k)):
  for r in range(min(n-l, k-l)):
    minus = []
    cur = 0
    for ll in range(l):
      cur += v[ll]
      if v[ll]<0:
        heappush(minus, v[ll])
    for rr in range(r):
      cur += v[n-rr-1]
      if v[n-rr-1]<0:
        heappush(minus, v[n-rr-1])
    rm = k-l-r
    while rm>0 and len(minus)>=rm:
      rm -= 1
      cur -= heappop(minus)
    ans = max(ans, cur)
print(ans)

Submission status: Wrong Answer

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

from heapq import heappush, heappop
for i in range(1, k+1):
  for j in range(0, i+1):
    minus = []
    l = min(n, j)
    r = min(n-l, i-l)
    cur = 0
    for ll in range(l):
      cur += v[ll]
      if v[ll]<0:
        heappush(minus, v[ll])
    for rr in range(r):
      cur += v[n-rr-1]
      if v[n-rr-1]<0:
        heappush(minus, v[n-rr-1])
    rm = k-l-r
    while rm>0 and len(minus)>=rm:
      rm -= 1
      t = heappop(minus)
      cur -= t
    ans = max(ans, cur)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
ans = 0
ls = [0 for i in range(n+1)]
rs = [0 for i in range(n+1)]
for i in range(n):
  ls[i+1] = ls[i]+v[i]
  rs[i+1] = rs[i]+v[n-i-1]

from heapq import heappush, heappop
for j in range(1, k+1):
  for h in range(0, j+1):
    minus = []
    l = min(n, h)
    r = min(n-l, j-l)
    for ll in range(l):
      if v[ll]<0:
        heappush(minus, v[ll])
    for rr in range(r):
      if v[n-rr-1]<0:
        heappush(minus, v[n-rr-1])
    cur = ls[l] + rs[r]
    rm = k-l-r
    while rm>0 and len(minus)>=rm:
      rm -= 1
      t = heappop(minus)
      cur -= t
    ans = max(ans, cur)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
W = [0]*N
aplus = [0]
bplus = [0]
anslist = []
for i in range(min(N,K)):
    aplus.append(aplus[i]+V[i])
    bplus.append(bplus[i]+V[N-i-1])
for i in range(min(N,K)):
    minuslist = []
    for l in range(i+1):
        if (l > 0)&(V[l-1] < 0):
            minuslist.append(V[l-1])
    for j in range(min(N,K-i)):
        if (j > 0)&(V[-j] < 0):
            minuslist.append(V[-j])
        minuslist.sort()
        if K-i-j > len(minuslist):
            anslist.append(aplus[i]+bplus[j]-sum(minuslist))
        else:
            minus = 0
            for k in range(K-i-j):
                minus += minuslist[k]
                anslist.append(aplus[i]+bplus[j]-minus)
            
print(max(anslist))

Submission status: Wrong Answer

Solution code:
def subsolve(pop, push):
    for i in range(pop + 1):
        rpop, lpop = i, pop - i
    return sum(sorted(vs[:lpop] + vs[-rpop:])[push:])


NEGINF = -float("inf")
n, k = map(int, input().split())
vs = list(map(int, input().split()))
ans = NEGINF

for i in range(k):
    pop, push = i, k - i
    if pop > push or pop > n:
        break
    ans = max(ans, subsolve(pop, push))
    
print(ans)

Submission status: Wrong Answer

Solution code:
jw , tr = map(int,input().split())
ans = 0
d = tuple(map(int,input().split()))
for take in range (1,min(51,tr+1)) :
    for l in range(0,take+1):
        if l == 0 :
            j = d[jw-take:]
        elif l == take :
            j = d[:l]
        else:
            j = d[:l] + d[jw-take+l:]
        j = list(j)
        j.sort()
        minus = len([i for i in j if i < 0])
        modosu = min(minus,tr-take)
        nans = sum(j[modosu:])
        ans = max(ans,nans)
print(ans)