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:
inpl = lambda: list(map(int,input().split()))
N, K = inpl()
V = inpl()
Nneg = len(list(filter(lambda x: x < 0, V)))
Vneg = sorted(list(enumerate(V)),key=lambda x: x[1])[:Nneg]

ans = 0
for i in range(Nneg+1):
    L = K - i
    if L > N:
        continue
    for j in range(L):
        S = sum(V[:L-j]) + sum(V[N-j:])
        k = 0
        for n, v in Vneg:
            if k >= i:
                break
            elif n < L-j or N-j <= n:
                S -= v
                k += 1
        if S > ans:
            ans = S

print(ans)

Submission status: Wrong Answer

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

def func(num, mode):
    ans = 0
    for i in range(1, num  +1):
        if mode == 0:
            tmp = vs[:i]
        else:
            tmp = vs[-i:]
        tmp.sort()
        # print(tmp, num, i, num - i)
        for j in range(num - i):
            if not tmp or tmp[0] >= 0:
                break
            tmp.pop(0)

        # print(tmp)
        ans = max(ans, sum(tmp))

    return ans



ans = 0
for i in range(k + 1):
    tmp = func(i, 0) + func(k - i, 1)
    ans = max(ans, tmp)
    # print(tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2000000)
def do_action(q, temoto, value, k_remain):
    if k_remain == 0:
        return value
    a = b = c = d = e = -100000000

    # どっちを掘るか決める
    l_sum = r_sum = 0

    for i in range(min(k_remain, len(q))):
        if q[i] > 0:
            l_sum += q[i]
        if q[- (i + 1)] > 0:
            r_sum += q[- (i + 1)]

    # a
    if l_sum >= r_sum:
        if len(q) != 0:
            val = q[0]
            a = do_action(q[1:], temoto + [val], value + val, k_remain - 1)
    # b
    if r_sum <= l_sum:
        if len(q) != 0:
            val = q[-1]
            b = do_action(q[:-1], temoto + [val], value + val, k_remain - 1)

    temoto.sort()
    # 戻す可能性があるのは負のがあるときのみ
    if len(temoto) > 0 and temoto[0] < 0:
        # c
        if len(temoto) != 0:
            temoto.sort()
            val = temoto[0]
            c = do_action([val] + q , temoto[1:], value - val, k_remain - 1)
        # d
        if len(temoto) != 0:
            temoto.sort()
            val = temoto[0]
            d = do_action(q + [val], temoto[1:], value - val, k_remain - 1)
    # e
    e = do_action(q , temoto, value, k_remain - 1)

    return max(a,b,c,d,e)

n, k = map(int, input().split())
dat_v = map(int, input().split())
dat_v = list(dat_v)
val = do_action(dat_v, [], 0, k)
print(val)

Submission status: Wrong Answer

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

def func(num, mode):
    ans = 0
    for i in range(1, num  +1):
        if mode == 0:
            tmp = vs[:i]
        else:
            tmp = vs[-i:]
        tmp.sort()
        for j in range(num - i):
            if not tmp or tmp[0] >= 0:
                break
            tmp.pop(0)

        ans = max(ans, sum(tmp))

    return ans


ans = 0
for i in range(k + 1):
    tmp = func(i, 0) + func(k - i, 1)
    ans = max(ans, tmp)
    print(tmp)
print(ans)

Submission status: Wrong Answer

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

value = max_value = 0
for j in range(K//2+1):
    for i in range(K-j+1):
        if i == K-j:
            value = V[:K-j]
        else:
            value = V[:i]+V[-(K-j-i):]
        value.sort()
        
        
        j = min(len(value)-1,j)
        if value[j] > 0:
            s = sum([i for i in value if i>0])
        else:
            s = sum(value[j:])
                
        if  s > max_value:
            max_value = s

print(max_value)

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,K+1):
    for j in range(i+1):
        rec = V[:i-j]+V[N-j:]
        rec.sort(reverse=True)
        away = min(i, K-i)
        for l in range(away+1):
            recsum = sum(rec[:i-l])
            if recsum > ans:
                ans=recsum
print(ans)

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 = cum[n]
for i in range(n):
    for j in range(n):
        if i > j:
            continue
        if n - (j - i + 1) > k:
            continue
        a = v[:i] + v[j+1:]
        a.sort()
        tmp = cum[n] - (cum[j+1] - cum[i])
        for x in range(k -(n-(j-i+1))):
            if x >= len(a) or a[x] >= 0:
                break
            tmp -= a[x]
        ans = max([ans, tmp])

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq


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

ans = 0
for kr in range(min(N, K)):
    for kl in range(min(N, K - kr)):
        kp = K - kr - kl
        # print(kr, kl, kp)
        if kr != 0:
            vs = Vs[-kr:] + Vs[:kl]
        else:
            vs = Vs[:kl]
        
        length = len(vs)
        if length == 0:
            continue
        heapq.heapify(vs)
        for i in range(min(kp, length)):
            if vs[0] < 0:
                heapq.heappop(vs)
            else:
                break
        value = sum(vs)
        ans = max(value, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = [int(i) for i in input().split()]
ans = 0
for left in range(k+1):
    for right in range(k - left + 1):
        back = k - left - right
        if left + right <= n:
            vv = v[:left] + v[n - right:]
            vv.sort()
            ans = max(sum(vv[back:]), ans)
        else:
            vv = v
            vv.sort()
            back = k - len(vv)
            ans = max(sum(vv[back:]), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))
V=sorted(v,reverse=True)
cnt=0
for i in range(n):
  if V[i]>0:
    cnt+=1
  else:
    break
print(sum(V[:(min(cnt,k//2))]))

Submission status: Wrong Answer

Solution code:
from itertools import*
import math
from collections import*
from heapq import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = float("inf")
mod = 10**9+7
from functools import reduce
import sys
sys.setrecursionlimit(10**7)

n,k = map(int,input().split())
D = deque()
V = list(map(int,input().split()))
for i in range(n):
    D.append(V[i])
ans = 0
if 2*n <= k:
    for j in range(n):
        if V[j]>0:
            ans += V[j]
    print(ans)
else:
    for i in range(k+1):
        for j in range(k+1):
                if i + j> k:
                    continue
                else:
                    cur = 0
                    V1 =[]
                    if i+j >=n:
                        cur = sum(V)
                        for v in V:
                            if v < 0:
                                V1.append(v)
                    else:
                        cur += sum(V[:i])
                        cur += sum(V[n-j:])
                        for s in range(i):
                            if V[s]<0:
                                V1.append(V[s])
                        for t in range(j):
                            if V[n-1-t]<0:
                                V1.append(V[n-1-t])
                    V1.sort(reverse=True)
                    for u in range(k-i-j):
                        if len(V1)>0:
                            cur -= V1.pop()
                        else:
                            break
                    ans = max(cur,ans)
print(ans)

Submission status: Wrong Answer

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

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

max_score = - (10**8)
searched_list = []
for k in range(K+1):
    #print(k)
    for l in range(k+1):
        for r in range(k+1-l):
            taken = V[:l]
            taken.extend(V[N-r:])
            sorted_taken = sorted(taken)
            #print(sorted_taken)
            neg_count = len([n for n in sorted_taken if n < 0])
            remove_count = min(k-l-r, neg_count)
            score = sum(sorted_taken[remove_count:])
            if max_score < score:
                max_score = score
                #print(sorted_taken)
                #print(remove_count)
print(max_score)

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, 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
        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())
Vs = list(map(int, input().split()))
max_v = 0
for k in range(0, K+1):
  for i in range(0, k+1):
    for j in range(0, K-k+1):
      inh = sorted(Vs[:i] + Vs[i+N-k:])
      max_v = max(max_v, sum(inh[j:]))

print(max_v)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from heapq import heappop

n, k = li()
v = list(li())

ans = 0

for x in range(k//2):
    get = k - x
    if get >= n:
        cnt = 0
        heap = sorted(v)

        while heap[0] < 0 and cnt < x:
            _ = heappop(heap)
            cnt += 1

        ans = max(ans, sum(heap))

    else:
        for i in range(get+1):
            cnt = 0
            heap = sorted(v[:i] + v[n-(get-i):])
            while heap[0] < 0 and cnt < x:
                _ = heappop(heap)
                cnt += 1

            ans = max(ans, sum(heap))

print(ans)

Submission status: Wrong Answer

Solution code:
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = s_inpl()   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = inpl()	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

import math
import copy
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
from bisect import bisect_left, bisect_right
# import numpy as np

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]

def i_inpl(): return int(input())
def s_inpl(): return map(int,input().split())
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")

############
############
############


N, K = s_inpl()
V = l_inpl()

def dfs(i, cnt, sum_val, lim, v):
    if cnt==lim or i == N-1:
        return sum_val
    elif cnt > lim:
        return -INF
    
    return max(dfs(i+1, cnt+1, sum_val+v[i], lim, v), dfs(i+1, cnt+2, sum_val, lim, v), sum_val)

ans = -INF
for i in range(K+1):
    lk, rk = i, K-i
    ans = max(ans, dfs(0, 0, 0, lk, V)+dfs(0, 0, 0, rk, V[::-1]))

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq


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

ans = 0
for kr in range(min(N, K)):
    for kl in range(min(N, K) - kr):
        kp = K - kr - kl
        # print(kr, kl, kp)
        if kr != 0:
            vs = Vs[-kr:] + Vs[:kl]
        else:
            vs = Vs[:kl]
        
        length = len(vs)
        if length == 0:
            continue
        heapq.heapify(vs)
        for i in range(min(kp, length)):
            if vs[0] < 0:
                heapq.heappop(vs)
            else:
                break
        value = sum(vs)
        ans = max(value, ans)

print(ans)

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 i in range(K+2):
    for r in range(i):
        for l in range(i-r):
            for ri in range(i-r-l):
                ans = max(ans,sum(sorted(V[0:r])[ri:]) + sum(sorted(V[N-l:])[i - r - l- ri-1:]))
#                print(r,l,ri,i-r-l-ri-1,sorted(V[0:r])[ri:],sorted(V[N-l:])[i - r - l- ri-1:])

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-



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

def get_sum(a,b):
  # a個取って、b個捨てる
  ret = 0
  for i in range(a+1):
    v = sorted(Vn[:i]+Vn[N-a+i:])
    r = sum(v)
    for i in range(min(b,len(v))):
      if v[i] <= 0:
        r -= v[i]
    ret = max(ret, r)
  return ret


def main():
  ans = 0
  for n in range(min(N+1,K)):
    ans = max(ans, get_sum(n, max(K-n,0)))
  return ans


if __name__ == "__main__":
  print(main())

Submission status: Wrong Answer

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

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

dp = [[[-1000000] * (K+1) for _ in range(N+1)] for _ in range(N+1)]
dp[0][N][0] = 0
for i in range(N) :
    for j in range(N, i-1, -1) :
        for k in range(1, K+1) :
            dp[i][j][k] = max(dp[i][j][k], dp[i][j][k-1])
            if 0 <= i-1 < N:
                dp[i][j][k] = max(dp[i][j][k], dp[i-1][j][k-1] + V[i-1])
            if 0 <= j+1 <= N :
                dp[i][j][k] = max(dp[i][j][k], dp[i][j+1][k-1] + V[j])
            if k >= 2 :
                if 0 <= i-1 < N :
                    dp[i][j][k] = max(dp[i][j][k], dp[i-1][j][k-2])
                if 0 <= j+1 <= N :
                    dp[i][j][k] = max(dp[i][j][k], dp[i][j+1][k-2])

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

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate,chain,product
N,K = map(int,input().split())

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

av = tuple(chain((0,),accumulate(V)))


def left():
    yield (0,)
    for i in range(1,N+1):
        tmp = sorted(min(0, v) for v in V[:i])
        yield tuple(chain((0,), accumulate(tmp)))

left_cost = tuple(left())


def right():
    yield (0,)
    for i in reversed(range(N)):
        tmp = sorted(min(0, v) for v in V[i:])
        yield tuple(chain((0,), accumulate(tmp)))

right_cost = tuple(right())

def helper():
    for i in range(N):
        for j in range(N-i):
            if i+j > K:
                continue
            d = K - i - j
            rev = av[i]+(av[N]-av[N-j])
            for k in range(d+1):
                yield rev - left_cost[i][min(i,k)] - right_cost[j][min(0,d-k)]
print(max(helper()))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
v = list(map(int,input().split()))
a = 0 #左からaことる
b = 0 #右からbことる

ans = -10**10
while a <= min(k,n):
    while b <= min(k-a,n-a):
        plus = []
        minus = []
        for i in range(a):
            if v[i] >= 0:
                plus.append(v[i])
            else:
                minus.append(v[i])
        for i in range(b):
            if v[-1-i] >= 0:
                plus.append(v[-1-i])
            else:
                minus.append(v[-1-i])
        plus.sort()
        minus.sort()
        temp = sum(plus) + sum(minus)
        m = k-a-b #取り出した後に残った操作回数
        for i in range(min(m,len(minus))):
            temp += -minus[i]
        m = m - min(m,len(minus)) #負の数を戻した後に残った操作回数
        if m != 0:
            if m%2 != 0:
                if len(minus) == 0 and len(plus) == 0:
                    temp = 0
                elif len(minus) == 0:
                    temp -= plus[0]
                elif len(plus) == 0:
                    temp += minus[-1]
                else:
                    if abs(minus[-1]) <= plus[0]:
                        temp += minus[-1]
                    else:
                        temp -= plus[0]
        if temp > ans:
            ans = temp
        b += 1
    a += 1
    b = 0
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = [int(i) for i in input().split()]
ans = 0
for left in range(k + 1):
    for right in range(k - left + 1):
        back = k - left - right
        if left + right <= n:
            vv = v[:left] + v[n - right:]
        else:
            vv = v
        vv.sort()
        ans = max(sum(vv[back:]), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
l = [0]
for i in range(1, K + 1): #左に何回操作するか
    #print("i=",i)
    b = 0
    for j in range(i // 2 + 1): #いくつ捨てるか
        if i - j > N:
            continue
        #print("j=",j)
        a = []
        for m in range(i - j):
            a.append(V[m])
        a.sort()
        s = 0
        for m in range(i - j):
            if m >= j or a[m] >= 0:
                s += a[m]
        b = max(b, s)
    #print("b=",b)
    l.append(max(b, l[-1]))
#print(l)
r = [0]
for i in range(1, K + 1): #右に何回操作するか
    #print("i=",i)
    b = 0
    for j in range(i // 2 + 1): #いくつ捨てるか
        if i - j > N:
            continue
        #print("j=",j)
        a = []
        for m in range(N - i + j, N):
            a.append(V[m])
        #print("s=",s)
        a.sort()
        s = 0
        for m in range(i - j):
            if m >= j or a[m] >= 0:
                s += a[m]
        b = max(b, s)
    #print("b=",b)
    r.append(max(b, r[-1]))
#print(r)
ans = 0
for i in range(K + 1):
    ans = max(ans, l[i] + r[K - i])
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
Vs = list(map(int, input().split()))
max_v = 0
for k in range(0, K+1):
  for i in range(0, k+1):
    for j in range(0, K-k+1):
      inh = sorted(Vs[:i] + Vs[i+N-k:])
      max_v = max(max_v, sum(inh[K-k:]))

print(max_v)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K, V):
    ans = 0
    for i in range(0, K//2):
        for j in range(i, K+1):
            l = V[:j-i]
            r = V[N-(K-j):]
            # print(j, K-j, l, r, K-i)
            if len(l) + len(r) > N:
                lr = []
                v = sorted(V, reverse=True)
                for k in range(len(l) + len(r)-N):
                    if v[k] > 0:
                        lr.append(v[k])
                    else:
                        break
                ans = max(ans, sum(lr))
            else:
                lr = l + r
                ans = max(ans, sum(sorted(lr,  reverse=True)[:K-2*i]))

        # print(i)
    return ans


def main():
    N, K = read_int_n()
    V = read_int_n()
    print(slv(N, K, V))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = [int(i) for i in input().split()]
L = [0] * (N+1)
R = [0] * (N+1)
L_m = [[] for _ in range(N+1)]
R_m = [[] for _ in range(N+1)]

for i in range(1, N+1):
    L[i] = L[i-1] + V[i-1]
    R[i] = R[i-1] + V[-i]
    L_m[i] = L_m[i-1].copy()
    R_m[i] = R_m[i-1].copy()
    if V[i-1] < 0:
        L_m[i].append(V[i-1])
    if V[-i] < 0:
        R_m[i].append(V[-i])

ans = 0

for i in range(K+1):
    for l in range(K+1-i):
        r = K - i - l
        if l + r > N or r < 0:
            break
        M = L_m[l] + R_m[r]
        M.sort()
        if len(M) >= i:
            m = sum(M[:i])
            a = L[l] + R[r] - m
            ans = max(ans, a)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

ans = 0

if N < K :
    K = N
for a in range(1,K+1) :
    for i in range(a+1) :
        G = []
        G.extend(V[:i])
        G.extend(V[len(V)-(a-i):])
        G.append(0)
        G = sorted(G)
        #bi = bisect.bisect(G,0)
        #G = G[min(bi-1,K-a):]

        cnt = 0
        calc = 0
        for i in range(len(G)) :
            if G[i] < 0 :
                if cnt < K-a :
                    cnt += 1
                else :
                    calc += G[i]
            else :
                calc += G[i]


        ans = max(ans,calc)

print(ans)

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 = cum[n]
for i in range(n):
    for j in range(n):
        if i >= j:
            continue
        if n - (j - i + 1) > k:
            continue
        a = v[:i] + v[j+1:]
        a.sort()
        tmp = cum[n] - (cum[j+1] - cum[i])
        for x in range(k -(n-(j-i+1))):
            if x >= len(a) or a[x] >= 0:
                break
            tmp -= a[x]
        ans = max([ans, tmp])

print(ans)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    import sys
    input = sys.stdin.readline

    import copy


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

    vv = list(map(int, input().split()))
    ans = 0


    for l in range((k+1)//2, k+1):
        for i in range(l+1):
            get_list = vv[:i]+ vv[n-(l-i):]
            get_list.sort(reverse=True)
            ans = max([ans, sum(get_list[:2*l-k])])

    vv.append(0)
    n += 1

    for l in range((k+1)//2, k+1):
        for i in range(l+1):
            get_list = vv[:i]+ vv[n-(l-i):]
            get_list.sort(reverse=True)
            ans = max([ans, sum(get_list[:2*l-k])])
    print(ans)

Submission status: Wrong Answer

Solution code:
list=input().split(" ")
n=int(list[0])
k=int(list[1])

list=input().split(" ")
for l in range(len(list)):
    list[l]=int(list[l])

#print(list)
maxpoint=0

for i in range(k+1):
    for j in range(i+1):
        #point=sum(list[:j])+sum(list[j-i:])
        if j==0:
            if j-i==0:
                #print([], [])
                juery=[]
            else:
                #print([], list[j-i:])
                juery=list[j-i:]
        else:
            if j-i==0:
                #print(list[:j], [])
                juery=list[:j]
            else:
                #print(list[:j], list[j-i:])
                juery=list[:j]+list[j-i:]
        #print(juery, sum(juery), k-len(juery))
        juery=sorted(juery)
        #print(juery, sum(juery), k-len(juery))
        point=0
        magic=min(k-len(juery), len(juery))
        for x in range(magic):
            if juery[x]<0:
                point-=juery[x]
        point+=sum(juery)
        #print(juery, point)
        if point>maxpoint:
            maxpoint=point

print(maxpoint)

Submission status: Wrong Answer

Solution code:
import bisect


def f(a: list, n: int) -> list:
    i = bisect.bisect(a, 0)
    return a[min(i, n):]


assert f([1, 2, 3], 1) == [1, 2, 3]
assert f([-1, 2, 3], 1) == [2, 3]
assert f([-2, -1, 3], 1) == [-1, 3]
assert f([-2, 0, 3], 1) == [0, 3]
assert f([-2, -1, 3], 3) == [3]


def main():
    # pass
    N, K = map(int, input().split())
    V = [int(x) for x in input().split()]
    Vr = list(reversed(V))

    ans = -float("inf")
    M = min(N, K)
    for l in range(0, M+1):
        for r in range(0, M+1):
            n = max(0, K - r - l)  # 戻すことができる最大の回数

            # dequeが空の場合は飛ばす
            if N < l + r:
                continue

            # l, r個ずつ取り出す
            a1 = V[:l]
            a2 = Vr[:r]
            a = a1 + a2
            a = sorted(a)

            ans = max(ans, sum(f(a, n)))
            # i = bisect.bisect(a, 0)
            #
            # b = a[min(i, n):]
            # score = sum(b)
            # ans = max(ans, score)

            # print(f"{l} {r} {n} {a1}+{a2}={a}")
            # print(i, a[:i], b)
            # print(f"score={score}")

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
(N,K) = list(map(int,input().split()))
Vlist = list(map(int,input().split()))
maxValue = -9999999999
K = K + 1
for x in range(K):
    for y in range(min(K-x, len(Vlist)-x)):
        for z in range(K-x-y):
            #print("%d,%d,%d"%(x,y,z))
            getlist = Vlist[:x] + list(reversed(Vlist))[:y]
            minuslis = sorted([a for a in getlist if (a<0)])
            sumV = sum(getlist)
            for i in range(min(z, len(minuslis))):
                sumV = sumV - minuslis[i]
           # print(sumV)
            #print()
            if maxValue < sumV:
                maxValue = sumV
print(maxValue)

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 // 2 + 1):
    if k - i <= n:
        for j in range(k - i + 1):
            l = v[:k - i - j] + v[n - j:]
            l = sorted(l)[::-1]
            w = sum(l[:k - i - i])
            if ans < w:
                ans = w
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

N,K = inpl()
vv = inpl()

ans = 0
for lnum in range(K+1):
    for rnum  in range(K-lnum+1):
        if lnum + rnum >= N:
            continue
        torukazu = vv[:lnum] + vv[N-rnum:]
        torukazu.sort()
        #print(' ' , torukazu,lnum,rnum)
        for sute in range(K-lnum-rnum+1):
            #print(torukazu[sute:])
            tmp = sum(torukazu[sute:])
            ans = max(tmp,ans)

print(ans)

Submission status: Wrong Answer

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

get, rel = 0, 0
ans = 0
for a in range(K + 1):  # a回行う
    for i in range(a + 1):
        rel = i
        get = a - i
        if rel == get:
            break
        right, left = 0, 0
        for j in range(get + 1):
            point = 0
            r, l = [], []
            right = j
            left = get - right
            l = V[:left]
            r = V[N - right:]
            al = r + l
            al.sort()
            point = sum(al[rel:])
            ans = max(ans, point)
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 // 2):
    if k - i <= n:
        for j in range(k - i + 1):
            l = v[:k - i - j] + v[n - j:]
            l = sorted(l)[::-1]
            w = sum(l[:k - i - i])
            if ans < w:
                ans = w
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = [int(i) for i in input().split()]
ans = 0
for left in range(k):
    for right in range(k - left):
        back = k - left - right
        if left + right <= n:
            vv = v[:left] + v[n - right:]
        else:
            vv = v
        vv.sort()
        ans = max(sum(vv[back:]), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
n,k=map(int,input().split())
V=[int(i) for i in input().split()]
M=sum(v for v in V if v>=0)
A=[[0,0]]
for i in range(1,n):
  VS=V[:i]
  cost=i
  A.append([cost,sum(VS)])
  heapq.heapify(VS)
  for _ in range(i):
    cost+=1
    heapq.heappop(VS)
    A.append([cost,sum(VS)])
B=[[0,0]]
V=V[::-1]
for i in range(1,n):
  VS=V[:i]
  cost=i
  B.append([cost,sum(VS)])
  heapq.heapify(VS)
  for _ in range(i):
    cost+=1
    heapq.heappop(VS)
    B.append([cost,sum(VS)])
ans=-float("inf")
for a in A:
  for b in B:
    if a[0]+b[0]<=k:
      ans=max(ans,a[1]+b[1])
print(min(ans,M))