Atcoder abc128 D

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
#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,greater >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;
}

你可能感兴趣的:(Acm,模拟,暴力)