1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| #include <cstdio> using namespace std; const int N = 5e5 + 10; typedef long long LL; LL ans; int n, m, cnt, w[N]; struct Data { LL s; int t; friend Data max(Data x, Data y) { if (x.s > y.s || (x.s == y.s && x.t < y.t)) return x; return y; } } f[2][2]; void check(int x) { f[0][0] = f[0][1] = (Data){0, 0}; for (int i = 1; i <= n; i++) { f[i & 1][0] = max(f[i - 1 & 1][0], f[i - 1 & 1][1]); f[i & 1][1] = (Data){f[i - 1 & 1][0].s + w[i] - x, f[i - 1 & 1][0].t + 1}; } ans = max(f[n & 1][0], f[n & 1][1]).s; cnt = max(f[n & 1][0], f[n & 1][1]).t; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &w[i]); check(0); if (cnt <= m) { printf("%lld", ans); return 0; } int l = 0, r = 1e6, res; while (l <= r) { int mid = l + r >> 1; check(mid); cnt <= m ? (res = mid, r = mid - 1) : (l = mid + 1); } check(res); printf("%lld", ans + (LL)res * m); return 0; }
|