#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 5;
ll a[maxn], dp[maxn], prefix[maxn];
int n, k;
pair<ll, int> alien(ll s) {
dp[0] = 0;
vector<int> cnt(n + 1, 0);
for (int i = 1; i <= n; i++) {
dp[i] = dp[i - 1];
cnt[i] = cnt[i - 1];
ll total = 0;
for (int j = i; j >= 1; j--) {
total += a[j];
if (dp[j - 1] + total - s > dp[i]) {
dp[i] = dp[j - 1] + total - s;
cnt[i] = cnt[j - 1] + 1;
}
}
}
return {dp[n] + k * s, cnt[n]};
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
ll l = -1e9, r = 1e9, ans = LLONG_MIN;
while (l <= r) {
ll m = (l + r) / 2;
auto [val, cnt_seg] = alien(m);
if (cnt_seg > k)
l = m + 1;
else {
ans = val;
r = m - 1;
}
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |