#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } constexpr Int INF = 1'000'000'000'000'000'000; void segSet(int segN, vector &seg, int a, Int val) { seg[a += segN] = val; for (; a >>= 1; ) { seg[a] = max(seg[a << 1], seg[a << 1 | 1]); } } Int segGet(int segN, const vector &seg, int a, int b) { Int ret = -INF; for (a += segN, b += segN; a < b; a >>= 1, b >>= 1) { if (a & 1) chmax(ret, seg[a++]); if (b & 1) chmax(ret, seg[--b]); } return ret; } int N, K, M; vector A; int main() { for (; ~scanf("%d%d%d", &N, &K, &M); ) { A.resize(N); for (int i = 0; i < N; ++i) { scanf("%lld", &A[i]); } vector ASum(N + 1); ASum[0] = 0; for (int i = 0; i < N; ++i) { ASum[i + 1] = ASum[i] + A[i]; } vector> ps(N + 1); for (int i = 0; i <= N; ++i) { ps[i] = {ASum[i], i}; } sort(ps.begin(), ps.end()); vector qs(N + 1); for (int j = 0; j <= N; ++j) { qs[ps[j].second] = j; } int segN; for (segN = 1; segN < N + 1; segN <<= 1) {} vector seg0(segN << 1, -INF); vector seg1(segN << 1, -INF); vector crt(N + 1, -INF); crt[0] = 0; for (int k = 0; k < K; ++k) { vector nxt(N + 1, -INF); for (int i = k + 1; i <= N; ++i) { segSet(segN, seg0, qs[i - 1], crt[i - 1] - ASum[i - 1]); segSet(segN, seg1, qs[i - 1], crt[i - 1] + ASum[i - 1]); chmax(nxt[i], segGet(segN, seg0, 0, qs[i]) + ASum[i]); chmax(nxt[i], segGet(segN, seg1, qs[i] + 1, N + 1) - ASum[i]); if (i >= M) { segSet(segN, seg0, qs[i - M], -INF); segSet(segN, seg1, qs[i - M], -INF); } } // cerr<