#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int n, x;
const int N = 5010;
int a[N];
int maxseg[N]; // maxseg[i]表示长度为`i`的序列的最大和
int main() {
int tc;
cin >> tc;
while (tc--) {
cin >> n >> x;
for (int i = 0; i < n; ++i) cin >> a[i];
maxseg[0] = 0;
for (int i = 1; i <= n; ++i) maxseg[i] = -INF;
// Iterate over length `l`, find the max value within the segment
for (int l = 0; l < n; ++l) {
int sum = 0;
for (int r = l; r < n; ++r) {
sum += a[r];
maxseg[r - l + 1] = max(maxseg[r - l + 1], sum);
}
}
// If we want to increase `k` numbers, it optimal to increase number within
// the segment if possible, i.e, if(len <= k), the sum will be increased by
// k*x; else, the sum will be increased by len*x
//
// So the problem is effectively turned into a problem to find the maximal
// sum of subsequence of length ranging from 1 to n
for (int k = 0; k <= n; ++k) {
int best = 0;
for (int len = 0; len <= n; ++len)
best = max(best, maxseg[len] + min(k, len) * x);
cout << best << ' ';
}
cout << endl;
}
}
Codeforces Educational Round 123 C Increase Subarray Sums
最新推荐文章于 2024-07-31 12:23:22 发布