//#define _GLIBCXX_DEBUG #include using namespace std; #define endl '\n' #define lfs cout<= (ll)(n); i--) using ll = long long; using ld = long double; const ll MOD1 = 1e9+7; const ll MOD9 = 998244353; const ll INF = 1e18; using P = pair; template bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;} template bool chmax(T1 &a,T2 b){if(a void ans(bool x,T1 y,T2 z){if(x)cout< void debug(vector>&v,ll h,ll w){for(ll i=0;i&v,ll h,ll w){for(ll i=0;i void debug(vector&v,ll n){if(n!=0)cout< vector>vec(ll x, ll y, T w){ vector>v(x,vector(y,w));return v;} ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;} vectordx={1,-1,0,0,1,1,-1,-1}; vectordy={0,0,1,-1,1,-1,1,-1}; template vector make_v(size_t a,T b){return vector(a,b);} template auto make_v(size_t a,Ts... ts){ return vector(a,make_v(ts...)); } template ostream &operator<<(ostream &os, const pair&p){ return os << p.first << " " << p.second; } //mt19937 mt(chrono::steady_clock::now().time_since_epoch().count()); template< typename Monoid ,typename F> struct SegmentTree { int sz, n; vector< Monoid > seg; const F f; const Monoid M1; SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1), n(n){ sz = 1; while(sz < n) sz <<= 1; seg.assign(2 * sz, M1); } void set(int k, const Monoid &x) { seg[k + sz] = x; } void build() { for(int k = sz - 1; k > 0; k--) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } void update(int k, const Monoid &x) { k += sz; seg[k] = x; while(k >>= 1) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } Monoid query(int a, int b) { if(a>=b)return M1; Monoid L = M1, R = M1; for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if(a & 1) L = f(L, seg[a++]); if(b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } template< typename C > int find_subtree(int a, const C &check, Monoid &M, bool type) { while(a < sz) { Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]); if(check(nxt)) a = 2 * a + type; else M = nxt, a = 2 * a + 1 - type; } return a - sz; } template< typename C > int find_first(int a, const C &check) { Monoid L = M1; if(a <= 0) { if(check(f(L, seg[1]))) return find_subtree(1, check, L, false); return -1; } int b = sz; for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if(a & 1) { Monoid nxt = f(L, seg[a]); if(check(nxt)) return find_subtree(a, check, L, false); L = nxt; ++a; } } return -1; } template< typename C > int find_last(int b, const C &check) { Monoid R = M1; if(b >= sz) { if(check(f(seg[1], R))) return find_subtree(1, check, R, true); return -1; } int a = sz; for(b += sz; a < b; a >>= 1, b >>= 1) { if(b & 1) { Monoid nxt = f(seg[--b], R); if(check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } void print(){ for(ll i=0;i struct SWAG{ stack>st_front,st_back; T func(T x, T y){return max(x,y);} void push(T x){ if(st_back.empty())st_back.emplace(x,x); else st_back.emplace(x,func(st_back.top().second,x)); } void pop(){ if(st_front.empty()){ while(!st_back.empty()){ if(st_front.empty())st_front.emplace(st_back.top().first,st_back.top().first); else st_front.emplace(st_back.top().first,func(st_back.top().first, st_front.top().second)); st_back.pop(); } } st_front.pop(); } T query(){ if(st_front.empty())return st_back.top().second; else if(st_back.empty())return st_front.top().second; else return func(st_front.top().second, st_back.top().second); } bool empty(){ return st_front.empty() && st_back.empty(); } int size(){ return st_front.size() + st_back.size(); } }; int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); ll res=0,buf=0; bool judge = true; ll n,k,m;cin>>n>>k>>m; vectora(n); rep(i,0,n)cin>>a[i]; vectorb(n+1); rep(i,0,n)b[i+1]=b[i]+a[i]; auto f=[&](ll x,ll y){return max(x,y);}; vector

p(n+1); rep(i,0,n+1)p[i]=MP(b[i],i); sort(ALL(p)); vectoridx(n+1); rep(i,0,n+1)idx[p[i].se]=i; vector> sw1(k+1),sw2(k+1); rep(i,1,k+1){ sw1[i].push(-INF); sw2[i].push(-INF); } sw1[0].push(0); sw2[0].push(0); rep(i,0,n){ if(i>=m){ rep(j,0,k+1){ sw1[j].pop(); sw2[j].pop(); } } rrep(j,1,k+1){ ll mx1=sw1[j-1].query(); ll mx2=sw2[j-1].query(); ll mx=max(mx1+b[i+1],mx2-b[i+1]); if(i==n-1&&j==k)res=mx; sw1[j].push(mx-b[i+1]); sw2[j].push(mx+b[i+1]); } sw1[0].push(-INF); sw2[0].push(-INF); } //debug(idx,n+1); cout<