This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
const int MAXN = 3e5 + 5;
int N, K, cnt[MAXN];
long long a[MAXN], dp[MAXN];
bool check(long long val) {
pair <long long, int> best(0, 0);
FOR(i, 1, N) {
dp[i] = dp[i - 1], cnt[i] = cnt[i - 1];
if(maximize(dp[i], best.first + a[i] - val)) {
cnt[i] = cnt[best.second] + 1;
}
if(maximize(best.first, dp[i] - a[i])) {
best.second = i;
}
}
return cnt[N] <= K;
}
void you_make_it(void) {
cin >> N >> K;
FOR(i, 1, N) cin >> a[i], a[i] += a[i - 1];
long long L = -1, R = 1E18;
while(R - L > 1) {
auto Mid = L + R >> 1LL;
if(check(Mid)) R = Mid;
else L = Mid;
}
check(R);
cout << dp[N] + 1LL * cnt[N] * R;
}
signed main() {
#ifdef LOCAL
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
#endif
auto start_time = chrono::steady_clock::now();
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
you_make_it();
auto end_time = chrono::steady_clock::now();
cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
// Dream it. Wish it. Do it.
Compilation message (stderr)
feast.cpp: In function 'void you_make_it()':
feast.cpp:44:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | auto Mid = L + R >> 1LL;
| ~~^~~
# | 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... |