#include using namespace std; typedef long long ll; #define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i) #define ALL(v) (v).begin(),(v).end() #define CLR(t,v) memset(t,(v),sizeof(t)) templateostream& operator<<(ostream& os,const pair&a){return os<<"("<void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<void chmin(T&a,const T&b){if(a>b)a=b;} templatevoid chmax(T&a,const T&b){if(a idx, data; SlideMin(int K) { this->K = K; } void move(int pos) { while (!idx.empty() && pos - idx.front() >= K) { idx.pop_front(); data.pop_front(); } } void push(ll val, int pos) { move(pos); while (!data.empty() && data.back() >= val) { // min: >=, max: <= idx.pop_back(); data.pop_back(); } idx.push_back(pos); data.push_back(val); } ll get(int pos) { bool dummy; return get(pos, dummy); } ll get(int pos, bool& empty) { // pos: right edge of the window (closed interval). move(pos); empty = data.empty(); if (empty) return 0; // return value is meaningless. return data.front(); } }; const ll INF = 1LL << 61; ll dp[3005][3005]; int main2() { REP(i, 3005) REP(j, 3005) dp[i][j] = -INF; int N = nextLong(); int K = nextLong(); int M = nextLong(); vector A(N), S(N+1); REP(i, N) A[i] = nextLong(); S[0] = 0; REP(i, N) S[i+1] = S[i] + A[i]; // pv(ALL(A)); // pv(ALL(S)); dp[0][0] = 0; for (int k = 1; k <= K; k++) { vector X(N+1), Y(N+1); for (int i = 0; i <= N; i++) { X[i] = dp[k-1][i] - S[i]; Y[i] = dp[k-1][i] + S[i]; } SlideMin sX(M), sY(M); for (int n = 1; n <= N; n++) { sX.push(-X[n-1], n-1); sY.push(-Y[n-1], n-1); ll val1 = -sX.get(n-1); ll val2 = -sY.get(n-1); ll val = max(val1 + S[n], val2 - S[n]); dp[k][n] = val; } // pv(dp[k], dp[k]+N+1); } ll ans = dp[K][N]; cout << ans << endl; return 0; } int main() { #ifdef LOCAL for (;!cin.eof();cin>>ws) #endif main2(); return 0; }