// - Art -
#include <bits/stdc++.h>
#define el cout << '\n'
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define REV(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define REP(i, c) for (int i = 0, _c = (c); i < _c; ++i)
const int N = 3e5 + 7;
using namespace std;
int n, k, a[N];
pair<long long, int> best(pair<long long, int> a, pair<long long, int> b) {
auto &[v1, c1] = a;
auto &[v2, c2] = b;
if (v1 > v2) {
return a;
}
else if (v1 < v2) {
return b;
}
return {v1, min(c1, c2)};
}
pair<long long, int> dp[N][2]; // first: val, second: cnt
pair<long long, int> Lagrangian(long long lamda) {
dp[0][0] = {0, 0};
dp[0][1] = {-1e18, 0}; /// Lỗi 1: không khởi gán
FOR (i, 1, n) {
dp[i][0] = best(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = best(make_pair(dp[i - 1][0].first + a[i] - lamda, dp[i - 1][0].second + 1), /// đoạn mới -> chịu phạt
make_pair(dp[i - 1][1].first + a[i], dp[i - 1][1].second));
}
return best(dp[n][0], dp[n][1]);
}
int main() {
#define name "art"
if (fopen(name".inp", "r")) {
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> k;
FOR (i, 1, n) {
cin >> a[i];
}
long long l = 0, r = 3e13, lamda = 0; /// lamda base = l, ko phải r
while (l <= r) {
long long m = l + (r - l) / 2;
if (Lagrangian(m).second >= k) {
l = m + 1;
lamda = m;
}
else {
r = m - 1;
}
}
auto [v, c] = Lagrangian(lamda);
cout << v + lamda * k, el;
return 0;
}
Compilation message (stderr)
feast.cpp: In function 'int main()':
feast.cpp:44:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:45:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | freopen(name".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... |