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 mnto(x, y) x = min(x, (__typeof__(x)) y)
#define mxto(x, y) x = max(x, (__typeof__(x)) y)
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb emplace_back
typedef vector<int> vi;
typedef vector<ii> vii;
#define INF 1000000005
#define LINF 100000000000000005
#define MOD 1000000007
#define MAXN 300005
int n, k;
int a[MAXN];
pll dp[MAXN], g[MAXN];
ll pre[MAXN];
pll check(ll x) {
// dp[i] = max(g[j] - pre[j]) + pre[i]
pll curMax = MP(0, 0);
REP (i, 1, n + 1) {
dp[i] = MP(curMax.FI + pre[i] - x, curMax.SE + 1);
g[i] = max(g[i - 1], dp[i]);
if (g[i].FI - pre[i] > curMax.FI || (g[i].FI - pre[i] == curMax.FI && g[i].SE < curMax.SE)) {
curMax = MP(g[i].FI - pre[i], g[i].SE);
}
}
return g[n];
}
int main() {
scanf("%d%d", &n, &k);
REP (i, 1, n + 1) scanf("%d", &a[i]);
REP (i, 1, n + 1) {
pre[i] = a[i] + pre[i - 1];
}
ll lo = 0, hi = LINF, mid;
while (lo < hi) {
mid = hi + lo >> 1;
pll temp = check(mid);
if (temp.SE <= k) {
hi = mid;
} else {
lo = mid + 1;
}
}
pll ans = check(hi);
printf("%lld\n", ans.FI + k * hi);
return 0;
}
Compilation message (stderr)
feast.cpp: In function 'int main()':
feast.cpp:51:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
51 | mid = hi + lo >> 1;
| ~~~^~~~
feast.cpp:44:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
44 | scanf("%d%d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~
feast.cpp:45:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
45 | REP (i, 1, n + 1) scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
# | 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... |