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>
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 3e5 + 10;
int n, k;
long long s[N];
pair <long long, int> f[N];
int calc(long long C) {
pair <long long, int> opt = {0, 0};
for (int i = 1; i <= n; ++i) {
f[i].first = opt.first + s[i] - C;
f[i].second = opt.second - 1;
if (f[i] < f[i - 1]) f[i] = f[i - 1];
if (make_pair(f[i].first - s[i], f[i].second) > opt)
opt = make_pair(f[i].first - s[i], f[i].second);
}
return - f[n].second;
}
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> s[i];
s[i] += s[i - 1];
}
if (calc(0) <= k) {
cout << f[n].first << '\n';
exit(0);
}
long long L = 0, R = 1e14;
while (L < R) {
long long mid = L + R >> 1;
if (calc(mid) <= k) R = mid;
else L = mid + 1;
}
calc(L);
cout << f[n].first + 1LL * k * L << '\n';
}
Compilation message (stderr)
feast.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
2 | #pragma GCC optimization ("O3")
|
feast.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
3 | #pragma GCC optimization ("unroll-loops")
|
feast.cpp: In function 'int main()':
feast.cpp:44:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | long long mid = L + R >> 1;
| ~~^~~
# | 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... |