Atcoder abc128 D

7 篇文章 0 订阅
4 篇文章 0 订阅

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 V1,V2,…,VN
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≤N≤50
1≤K≤100
−107≤Vi≤107
Input
Input is given from Standard Input in the following format:
N K
V1 V2 … VN
Output
Print the maximum possible sum of the values of jewels in your hands after the operations.

Sample Input 1
Copy
6 4
-10 8 2 1 2 6
Sample Output 1
Copy
14

思路:因为n和k都很小,所有可以直接暴力,只要枚举去1-k个的最大值就可以了。

代码:

#include<bits/stdc++.h>
#define ULL unsigned long long
#define LL long long
#define Max 105
#define mem(a,b) memset(a,(int)b,sizeof(a));
#define pb push_back
#define mp make_pair
#define debug printf("debug\n");
const LL mod=1e9+7;
const ULL base=131;
const LL LL_MAX=9223372036854775807;
using namespace std;
int n,k;
LL a[Max];
priority_queue<LL,vector<LL>,greater<LL> >p;
LL fun(int l,int r){
    for(int i=1;i<=l;i++)
        p.push(a[i]);
    for(int j=1,t=n;j<=r;j++,t--)
        p.push(a[t]);
    int opt=k-l-r;
    while(p.size() && p.top()<0 && opt)
        p.pop(),opt--;
    LL sum=0;
    while(p.size())
        sum+=p.top(),p.pop();
    return sum;
}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
    }
    LL sum=0;
    for(int i=0;i<=k;i++){
       for(int j=0;(i+j)<=min(n,k);j++){
            sum=max(fun(i,j),sum);
       }
    }
    printf("%lld\n",sum);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值