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:
#再帰関数でやろうとしたけど、よくわからなくなった

from collections import deque
import heapq
import sys

sys.setrecursionlimit(10**5)

N,K=map(int,input().split())
D=deque(map(int,input().split()))
jList=[]
ans=0
heapq.heapify(jList)

def jewelry(k,D,jList):
    if k==0:
        return sum(jList)

    elif D and jList==[]:

        s0=jewelry(k-1,D,jList)

        jList.append(D.popleft())
        s1=jewelry(k-1,D,jList)
        D.appendleft(jList.pop())

        jList.append(D.pop())
        s2=jewelry(k-1,D,jList)
        D.append(jList.pop())

        return max(s0,s1,s2)
        
    elif not D and jList!=[]:

        s0=jewelry(k-1,D,jList)

        MIN=heapq.heappop(jList)
        D.appendleft(MIN)
        s3=jewelry(k-1,D,jList)
        heapq.heappush(jList,D.popleft())

        MIN=heapq.heappop(jList)
        D.append(MIN)
        s4=jewelry(k-1,D,jList)
        heapq.heappush(jList,D.pop())

        return max(s0,s3,s4)

    
    elif D and jList!=[]:

        s0=jewelry(k-1,D,jList)

        jList.append(D.popleft())
        s1=jewelry(k-1,D,jList)
        D.appendleft(jList.pop())

        jList.append(D.pop())
        s2=jewelry(k-1,D,jList)
        D.append(jList.pop())

        MIN=heapq.heappop(jList)
        D.appendleft(MIN)
        s3=jewelry(k-1,D,jList)
        heapq.heappush(jList,D.popleft())

        MIN=heapq.heappop(jList)
        D.append(MIN)
        s4=jewelry(k-1,D,jList)
        heapq.heappush(jList,D.pop())

        return max(s0,s1,s2,s3,s4)

print(jewelry(K,D,jList))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
from collections import deque
d=deque(V)
K=min(N,K)
ans=-10**10
for k in range(0,K+1):
    for n in range(0,k+1):
        A=V[0:n]+V[N-(k-n):]
        if len(A)==0:
            A=[0]
        A.sort()
        tmp=sum(A)
        for i in range(0, min(K-k,len(A))):
            if A[i]<0:
                tmp-=A[i]
            else:
                break
        if ans<tmp:
            ans=tmp
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

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

ans = 0
for i in range(N+1):
    for j in range(N+1):
        if i + j > min(K, N+1):
            continue
        que = []
        for k in range(i):
            heapq.heappush(que, V[k])
        for k in range(j):
            heapq.heappush(que, V[-k-1])

        for l in range(K-i-j):
            if len(que) == 0 or que[0] > 0:
                break
            heapq.heappop(que)

        #print(i, j, K-i-j, que)

        ans = max(ans, sum(que))

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

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

ans = 0
for i in range(N):
    for j in range(N):
        if i + j >= min(K, N):
            continue
        que = []
        for k in range(i):
            heapq.heappush(que, V[k])
        for k in range(j):
            heapq.heappush(que, V[-k-1])

        for l in range(K-i-j):
            if len(que) == 0 or que[0] > 0:
                break
            heapq.heappop(que)

        #print(i, j, K-i-j, que)

        ans = max(ans, sum(que))

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

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

ans = 0
for i in range(N):
    for j in range(N):
        if i + j >= min(K, N):
            continue
        que = []
        for k in range(i):
            heapq.heappush(que, V[k])
        for k in range(j):
            heapq.heappush(que, V[-k-1])

        for l in range(K-i-j):
            if len(que) == 0:
                break
            heapq.heappop(que)

        ans = max(ans, sum(que))

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

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

ans = 0
for i in range(N):
    for j in range(N):
        if i + j >= K:
            continue
        que = []
        for k in range(i):
            heapq.heappush(que, V[k])
        for k in range(j):
            heapq.heappush(que, V[-k-1])

        for l in range(K-i-j):
            if len(que) == 0:
                break
            heapq.heappop(que)

        ans = max(ans, sum(que))

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
ret = 0
for a in range(min(N,K)+1):
  for b in range(min(N,K)-a+1):
    W=A[:a]+A[N-b:]
    W.sort()
    sl=min(sum([1 for i in W if i<0]),K-a-b)
    print(W,W[sl:],a,b,K-a-b)
    ret=max(ret,sum(W[sl:]))
print(ret)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
ret = 0
for a in range(K+1):
  for b in range(K-a+1):
    W=A[:a]+A[N-b:]
    W.sort()
    sl=min(sum([1 for i in W if i<0]),K-a-b)
    ret=max(ret,sum(W[sl:]))
print(ret)

Submission status: Wrong Answer

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(3000)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

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

ans = -sys.maxsize

for i in range((k+1)//2, k+1, 1):
    if i > n:
        continue

    for j in range(i+1):
        chosen = []
        for l in range(j):
            chosen.append(data[l])
        for l in range(i-j):
            chosen.append(data[n-1-l])

        if k-i == i:
            tmp_ans = 0
        else:
            chosen.sort()

            for l in range(k-i):
                if chosen[0] < 0:
                    del chosen[0]
            tmp_ans = sum(chosen)

        ans = max(ans, tmp_ans)

ans = max(ans, 0)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(3000)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

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

ans = -sys.maxsize

for i in range((k+1)//2, k+1, 1):
    if i > n:
        continue

    for j in range(i+1):
        chosen = []
        for l in range(j):
            chosen.append(data[l])
        for l in range(i-j):
            chosen.append(data[n-1-l])

        if k-i == i:
            tmp_ans = 0
        else:
            chosen.sort()
            tmp_ans = sum(chosen[k-i:i])
            if tmp_ans > 0:
                tmp_ans2 = sum(filter(lambda x: x > 0, chosen))
                tmp_ans = max(tmp_ans, tmp_ans2)

        ans = max(ans, tmp_ans)

ans = max(ans, 0)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(3000)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

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

ans = -sys.maxsize

for i in range((k+1)//2, k+1, 1):
    if i > n:
        continue

    for j in range(i+1):
        chosen = []
        for l in range(j):
            chosen.append(data[l])
        for l in range(i-j):
            chosen.append(data[n-1-l])

        if k-i == i:
            tmp_ans = 0
        else:
            chosen.sort()
            tmp_ans1 = sum(chosen[k-i:i])
            tmp_ans2 = sum(filter(lambda x: x > 0, chosen))
            tmp_ans = max(tmp_ans1, tmp_ans2)

        ans = max(ans, tmp_ans)

ans = max(ans, 0)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(3000)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

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

ans = -sys.maxsize

for i in range((k+1)//2, k+1, 1):
    if i > n:
        continue

    for j in range(i+1):
        chosen = []
        for l in range(j):
            chosen.append(data[l])
        for l in range(i-j):
            chosen.append(data[n-1-l])

        if k-i == i:
            tmp_ans = 0
        else:
            chosen.sort()
            tmp_ans = sum(chosen[k-i:i])
        ans = max(ans, tmp_ans)

ans = max(ans, 0)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(3000)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

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

ans = -sys.maxsize

for i in range((k+1)//2, k+1, 1):
    if i > n:
        continue

    for j in range(i+1):
        chosen = []
        for l in range(j):
            chosen.append(data[l])
        for l in range(i-j):
            chosen.append(data[n-1-l])

        if k-i == i:
            tmp_ans = 0
        else:
            chosen.sort()
            tmp_ans = sum(chosen[k-i:i])
        ans = max(ans, tmp_ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(3000)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

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

ans = -sys.maxsize

for i in range(k//2, k+1, 1):
    if i > n:
        continue

    for j in range(0, i, 1):
        chosen = []
        for l in range(j):
            chosen.append(data[l])
        for l in range(i-j):
            chosen.append((data[n-l-1]))
        chosen.sort()

        tmp_ans = sum(chosen[min(k-i, i):max(k-i, i)])
        ans = max(ans, tmp_ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(3000)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

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

ans = -sys.maxsize

for i in range(k//2 , k+1, 1 ):
    if i > n: continue

    for j in range(0, i+1, 1):
        chosen = []
        for l in range(j):
            chosen.append(data[l])
        for l in range(i-j):
            chosen.append((data[n-l-1]))
        chosen.sort()

        tmp_ans = sum(chosen[min(k-i, i):max(k-i, i)])
        ans = max(ans, tmp_ans)

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):
  l=min(k-i+1,n+1)
  for a in range(l):
    b=k-a-i
    if b==0:
      sub=v[:a]
    else:
      sub=v[:a]+v[-b:]
    h=min(len(sub),i)
    for j in range(h):
      m=min(sub)
      if m<0:
        sub.remove(m)
    ans=max(ans,sum(sub))

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
v=list(map(int,input().split()))
ans=0
if n < k:
  k=n
for i in range(k+1):
  for a in range(k-i+1):
    b=k-a-i
    if b==0:
      sub=v[:a]
    else:
      sub=v[:a]+v[-b:]
    h=min(len(sub),i)
    for j in range(h):
      m=min(sub)
      if m<0:
        sub.remove(m)
    ans=max(ans,sum(sub))

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):
  for a in range(k-i+1):
    b=k-a-i
    if b==0:
      sub=v[:a]
    else:
      sub=v[:a]+v[-b:]
    h=min(len(sub),i)
    for j in range(h):
      if len(sub)>0:
        m=min(sub)
        if m<0:
          sub.remove(m)
    ans=max(ans,sum(sub))

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):
  for a in range(k-i+1):
    b=k-a-i
    if b==0:
      sub=v[:a]
    else:
      sub=v[:a]+v[-b:]
    for j in range(i):
      if len(sub)>0:
        m=min(sub)
        if m<0:
          sub.remove(m)
    ans=max(ans,sum(sub))

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N,K=map(int,input().split())
    V=list(map(int,input().split()))
    M=min(N,K)
    result=0
    for l in range(M+1):
        for r in range(M+1-l):
            L=l
            R=r
            count=0
            minus=[]
            m=l+r
            while L>0:
                a=V[L-1]
                if a>=0:
                    count+=a
                else:
                    count+=a
                    minus.append(a)
                L-=1
            while R>0:
                a=V[-R]
                if a>=0:
                    count+=a
                else:
                    count+=a
                    minus.append(a)
                R-=1

            minus.sort()
            i_minus=0
            while M-m>0 and i_minus<len(minus):
                count-=minus[i_minus]
                m+=1
                i_minus+=1
            result=max(result,count)
    print(result)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    L = [(0, [])]
    R = [(0, [])]

    mp = 0
    mn = []
    for _, v in zip(range(K), V):
        if v >= 0:
            mp += v
        else:
            mn.append(v)
            mn.sort()
        L.append((mp, tuple(mn)))

    mp = 0
    mn = []
    for _, v in zip(range(K), reversed(V)):
        if v >= 0:
            mp += v
        else:
            mn.append(v)
            mn.sort()
        R.append((mp, tuple(mn)))

    mval = 0
    for l, (lp, ln) in enumerate(L):
        for r, (rp, rn) in zip(range(K - l + 1), R):
            if l + r + 1 == len(V):
                break
            val = rp + lp
            n = K - l - r
            if len(ln) + len(rn) > n:
                t = list(ln) + list(rn)
                t.sort()
                val += sum(t[n:])
            mval = max(mval, val)
    print(mval)


main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    L = [(0, [])]
    R = [(0, [])]

    mp = 0
    mn = []
    for _, v in zip(range(K), V):
        if v >= 0:
            mp += v
        else:
            mn.append(v)
            mn.sort()
        L.append((mp, tuple(mn)))

    mp = 0
    mn = []
    for _, v in zip(range(K), reversed(V)):
        if v >= 0:
            mp += v
        else:
            mn.append(v)
            mn.sort()
        R.append((mp, tuple(mn)))

    mval = 0
    for l, (lp, ln) in enumerate(L):
        for r, (rp, rn) in zip(range(K - l + 1), R):
            if l + r == len(V):
                break
            val = rp + lp
            n = K - l - r
            if len(ln) + len(rn) > n:
                t = list(ln) + list(rn)
                t.sort()
                val += sum(t[n:])
            mval = max(mval, val)
    print(mval)


main()

Submission status: Wrong Answer

Solution code:
import copy
if __name__ == '__main__':
    N,K=list(map(int,input().split()))
    Jewelries_org=list(map(int,input().split()))
    ans=0
    upper=min(N,K)
    for a in range(upper):
        for b in range(upper-a):
            hand=[]
            Jewelries=copy.deepcopy(Jewelries_org)
            for i in range(a):
                hand.append(Jewelries.pop(0))
            for i in range(b):
                hand.append(Jewelries.pop(-1))
            hand.sort()
            for i in range(K-(a+b)):
                if hand.__len__()>0 and hand[0]<0:
                    hand.pop(0)
            s=sum(hand)
            if s>ans:
                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 i in range(n+1):
    for j in range(n-i+1):
        print(i, j)
        if i+j>k:
            continue
        tmp=V[:i]+V[n-j:]
        tmp.sort(reverse=True)
        for l in range(k-i-j):
            if tmp and tmp[-1]<0:
                tmp.pop()
            else:
                break
        ans=max(ans,sum(tmp))
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush
from itertools import accumulate
N, K = map(int, input().split())
*V, = map(int, input().split())

ans = 0
for a in range(N+1):
    for b in range(N+1):
        if a+b>N:continue
        
        if a and b:
            qa, qb = list(accumulate([0]+sorted(V[:a]))), list(accumulate([0]+sorted(V[-b:])))
            sa, sb = sum(V[:a]), sum(V[-b:])
            for c in range(a+1):
                for d in range(b+1):
                    if a+b+c+d==K:
                        ans = max(ans, sa+sb-qa[c]-qb[d])
        elif a:
            qa = list(accumulate([0]+sorted(V[:a])))
            sa = sum(V[:a])
            for c in range(a+1):
                if a+c==K:
                    ans = max(ans, sa-qa[c])
        elif b:
            qb = list(accumulate([0]+sorted(V[-b:])))
            sb = sum(V[-b:])
            for d in range(b+1):
                if b+d==K:
                    ans = max(ans, sb-qb[d])

print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc128/tasks/abc128_d


# 単純なアイデアとしては、ほぼ愚直に操作を行う。
# K個のViを取って一番合計の大きいもの
# K-1のViを取って価値の引くものを1つ詰めるとしたときに一番合計の大きくなるもの
# K-iのViを取って価値の低いものをiつ詰めるとしたときに一番合計が大きくなるもの

# 最大N個とるのを最大N回ずらして、最大N個の手持ちのソートするので、オーダーはO(N^3 logN)
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


N, K = read_ints()
V = read_ints()
V = V + V

ans = 0
for i in range(K):
    n_pick = K - i
    n_back = i
    if n_pick > N:
        n_back = n_pick - N
        n_pick = N

    for j in range(n_pick):  # スライドよう
        v = V[N - j:N + n_pick - j]
        v.sort()
        ans = max(ans, sum(v[n_back:]))
print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc128/tasks/abc128_d


# 単純なアイデアとしては、ほぼ愚直に操作を行う。
# K個のViを取って一番合計の大きいもの
# K-1のViを取って価値の引くものを1つ詰めるとしたときに一番合計の大きくなるもの
# K-iのViを取って価値の低いものをiつ詰めるとしたときに一番合計が大きくなるもの

# 最大N個とるのを最大N回ずらして、最大N個の手持ちのソートするので、オーダーはO(N^3 logN)
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


N, K = read_ints()
V = read_ints()
V = V + V

ans = 0
for i in range(K):
    n_pick = K - i
    n_back = i
    if n_pick > N:
        n_back = n_pick - N
        n_pick = N

    for j in range(K):  # スライドよう
        v = V[N - j:N + n_pick - j]
        v.sort()
        ans = max(ans, sum(v[n_back:]))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

n, k, *v = map(int, sys.stdin.read().split())

def main():
    m = min(n, k)
    ans = 0
    for i in range(m+1):
        for j in range((m-i)+1):
            tmp = v[:i] + v[-j:] if j else v[:i]
            
            res = sum(tmp) if tmp else 0
            neg = [t for t in tmp if t < 0]
            neg.sort()
            res -= sum(neg[:m-(i+j)])
            ans = max(ans, res)
    return ans
   
if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys

n, k, *v = map(int, sys.stdin.read().split())

def main():
    m = min(n, k)
    ans = 0
    for i in range(m+1):
        for j in range((m-i)+1):
            tmp = v[:i] + v[-j:] if j else v[:i]
            
            res = sum(tmp) if tmp else 0
            neg = [t for t in tmp if t < 0]
            neg.sort()
            if m > i + j:
                res -= sum(neg[:m-(i+j)])
            ans = max(ans, res)
    return ans
   
if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys

n, k = map(int, sys.stdin.readline().split())
*v, = map(int, sys.stdin.readline().split())

def main():
    m = min(n, k)
    sl = [None] * (m + 1); sl[0] = 0
    sr = [None] * (m + 1); sr[0] = 0
    neg_l = [None] * (m + 1); neg_l[0] = []
    neg_r = [None] * (m + 1); neg_r[0] = []
    for i in range(m):
        l = v[i]
        r = v[-(i+1)]
        sl[i+1] = sl[i] + l
        sr[i+1] = sr[i] + r

        neg_l[i+1] = neg_l[i].copy()
        if l < 0:
            neg_l[i+1].append(l)
        neg_r[i+1] = neg_r[i].copy()
        if r < 0:
            neg_r[i+1].append(r)

    res = 0
    for i in range(m+1):
        for j in range((m-i)+1):
            s = sl[i]+ sr[j]
            neg = sorted(neg_l[i] + neg_r[j])[:m-(i+j)]
            if neg:
                s -= sum(neg)
            res = max(res, s)
    
    return res

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys

n, k = map(int, sys.stdin.readline().split())
*v, = map(int, sys.stdin.readline().split())

def main():
    m = min(n, k)
    sl = [None] * (m + 1); sl[0] = 0
    sr = [None] * (m + 1); sr[0] = 0
    neg_l = [None] * (m + 1); neg_l[0] = []
    neg_r = [None] * (m + 1); neg_r[0] = []
    for i in range(m):
        l = v[i]
        r = v[-(i+1)]
        sl[i+1] = sl[i] + l
        sr[i+1] = sr[i] + r

        neg_l[i+1] = neg_l[i].copy()
        if l < 0:
            neg_l[i+1].append(l)
        neg_r[i+1] = neg_r[i].copy()
        if r < 0:
            neg_r[i+1].append(r)

    res = 0
    for i in range(m+1):
        for j in range((m-i)+1):
            s = sl[i]+ sr[j]
            neg = sorted(neg_l[i] + neg_r[j])[:m-(i+j)]
            s -= 0 if not neg else sum(neg)
            res = max(res, s)
    
    return res

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
M = min(N, K)
ans = 0
for a in range(M):
    for b in range(M - a):
        tmp = []
        tmp.extend(V[:a])
        tmp.extend(V[::-1][:b])
        tmp.sort()
        for i in range(K - (a + b)):
            if len(tmp) > 0 and tmp[0] < 0:
                tmp.pop(0)
        ans = max(ans, sum(tmp))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
M = min(N, K)
ans = 0
for a in range(M):
    for b in range(M - a+1):
        tmp = []
        tmp.extend(V[:a])
        tmp.extend(V[::-1][:b])
        tmp.sort()
        for i in range(K - (a + b)):
            if len(tmp) > 0 and tmp[0] < 0:
                tmp.pop(0)
        ans = max(ans, sum(tmp))
print(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(min(k+1,n)):
  for r in range(min(k+1-l,n-l)):
    if l==0 and r==0:
      continue
    rem=k-l-r
    if l==0:
      arr=v[n-r:]
    elif r==0:
      arr=v[:l]
    else:
      arr=v[:l]+v[n-r:]
    arr.sort()
    m=sum(arr)
    idx=0
    while 0<=idx<len(arr) and arr[idx]<0 and rem>0:
      rem-=1
      m-=arr[idx]
      idx+=1
    ans=max(ans,m)
print(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(min(k+1-l,n-l)):
    if l==0 and r==0:
      continue
    rem=k-l-r
    if l==0:
      arr=v[n-r:]
    elif r==0:
      arr=v[:l]
    else:
      arr=v[:l]+v[n-r:]
    arr.sort()
    m=sum(arr)
    idx=0
    while 0<=idx<len(arr) and arr[idx]<0 and rem>0:
      rem-=1
      m-=arr[idx]
      idx+=1
    ans=max(ans,m)
print(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):
    if l==0 and r==0:
      continue
    rem=k-l-r
    if l==0:
      arr=v[n-r:]
    elif r==0:
      arr=v[:l]
    else:
      arr=v[:l]+v[n-r:]
    arr.sort()
    m=sum(arr)
    idx=0
    while 0<=idx<len(arr) and arr[idx]<0 and rem>0:
      rem-=1
      m-=arr[idx]
      idx+=1
    ans=max(ans,m)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n,k = map(int,readline().split())
lst1 = list(map(int,readline().split()))

ans = 0
for a in range(n+1):
    for b in range(n+1):
        if a+b >= min(n,k):
            continue
        left = lst1[:a]
        right = lst1[n-b:]
        res = left + right
        rest = k - (a + b)
        if rest > 0:
            res.sort()
            while rest:
                if len(res) == 0:
                    break
                if res[0] < 0:
                    del res[0]
                    rest -=1
                else:
                    break
        ans = max(ans,sum(res))

print(ans)