Codeforces Educational Round 123 C Increase Subarray Sums

#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;
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值