// Author: 4uckd3v - Nguyen Cao Duc
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_N = 1e6;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
int n, k;
int a[MAX_N + 5];
ll pref[MAX_N + 5];
ll getSum(int l, int r) {
return pref[r] - pref[l - 1];
};
namespace SUBTASK_3456 {
const int LIM = 1e8;
bool checkSubtask() {
return 1LL * n * k <= LIM;
};
void Solve() {
vector<ll> cur(n + 5, -INF), nxt(n + 5, -INF), mx(n + 5);
for (int i = 0; i <= n; i++) cur[i] = 0;
nxt[0] = 0;
ll res = 0;
for (int i = 1; i <= k; i++) {
mx[i - 1] = -INF;
for (int r = i; r <= n; r++) {
mx[r] = max(mx[r - 1], cur[r - 1] - pref[r - 1]);
nxt[r] = max(nxt[r - 1], mx[r] + pref[r]);
};
for (int r = i; r <= n; r++) {
cur[r] = nxt[r];
nxt[r] = -INF;
};
res = max(res, cur[n]);
};
cout << res << '\n';
};
}; // namespace SUBTASK_3456
namespace SUBTASK_7 {
const int N = MAX_N;
pair<ll, int> dp[N + 5];
pair<ll, int> calc(ll lambda) {
for (int i = 0; i <= n; i++) dp[i] = {0, 0};
pair<ll, int> mx = {0, 0};
dp[0] = {0, 0};
for (int i = 1; i <= n; i++) {
if (dp[i - 1].first - pref[i - 1] > mx.first)
mx = {dp[i - 1].first - pref[i - 1], dp[i - 1].second};
else if (dp[i - 1].first - pref[i - 1] == mx.first)
mx = {dp[i - 1].first - pref[i - 1], max(mx.second, dp[i - 1].second)};
if (dp[i - 1].first > mx.first + pref[i] - lambda) {
dp[i] = dp[i - 1];
} else if (dp[i - 1].first == mx.first + pref[i] - lambda) {
dp[i].first = mx.first + pref[i] - lambda;
dp[i].second = max(dp[i - 1].second, mx.second + 1);
} else {
dp[i].first = mx.first + pref[i] - lambda;
dp[i].second = mx.second + 1;
};
};
return dp[n];
};
void Solve() {
ll l = -3e14, r = 3e14, lambda = 0;
while (l <= r) {
ll mid = l + (r - l) / 2;
if (calc(mid).second >= k) {
lambda = mid;
l = mid + 1;
} else
r = mid - 1;
};
cout << max(0LL, calc(lambda).first + lambda * k) << '\n';
};
}; // namespace SUBTASK_7
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
if (fopen("MAIN.INP", "r")) {
freopen("MAIN.INP", "r", stdin);
freopen("MAIN.OUT", "w", stdout);
};
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
};
// SUBTASK_3456::Solve();
SUBTASK_7::Solve();
};
컴파일 시 표준 에러 (stderr) 메시지
feast.cpp: In function 'int main()':
feast.cpp:101:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | freopen("MAIN.INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:102:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | freopen("MAIN.OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |