标签:+= task signed push lin inline lan 操作 void
题意:给出序列\(n<=50\)长度大小,然后给出\(k <= 100\)操作,可以从头部或者尾部拿出一个元素放到手里或者将某个手里的元素塞到序列,求手里最大和。
题解:枚举从头拿和从尾拿的操作,然后可确定放手的次数,然后就贪心得放出负数的数,最后手里得数就是最大值。
代码:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#include <set>
using namespace std;
typedef long long ll;
ll a[ 3030];
priority_queue<int, vector<int>, greater<int>>pq;
void solve() {
int n, k;cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
ll ans = -999999999999999;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
if (i + j > k)continue;
if (i + j > n)continue;
while (!pq.empty())pq.pop();
int A = i, B = j;
int ii = 1;
while (A--) pq.push(a[ii++]);
int jj = n;
while (B--) pq.push(a[jj--]);
int re = k - (i + j);
while (re > 0 && !pq.empty() && pq.top() < 0) {
re--;
pq.pop();
//cout << pq.top() << "?";
}//cout << endl << endl;
ll s = 0;
while (!pq.empty()) {
s += pq.top();
pq.pop();
}
ans = max(ans, s);
}
}
cout << ans << endl;//cout << "??0";
}
signed main() {
ll t = 1;
//cin >> t;
while (t--) {
solve();
}
}
标签:+= task signed push lin inline lan 操作 void
原文地址:https://www.cnblogs.com/Xiao-yan/p/14483130.html