#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned int ui; const ll mod = 1000000007; const ll INF = (ll)1000000007 * 1000000007; typedef pair P; #define stop char nyaa;cin>>nyaa; #define rep(i,n) for(int i=0;i=0;i--) #define Rep(i,sta,n) for(int i=sta;i=sta;i--) #define rep1(i,n) for(int i=1;i<=n;i++) #define per1(i,n) for(int i=n;i>=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) typedef long double ld; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair LP; int dx[4]={1,-1,0,0}; int dy[4]={0,0,1,-1}; int n,m,k; ll a[3010],S[3010]; ll dp[3010][3010]; deque que1[3010],que2[3010]; void solve(){ cin >> n >> k >> m; rep(i,n+1){ rep(j,k){ dp[j][i]=-INF; } } rep(i,n){ cin >> a[i]; } rep(i,n){ S[i+1]=S[i]+a[i]; } dp[0][0]=0; rep(i,k){ que1[i].push_back(0); que2[i].push_back(0); } Rep(i,1,n+1){ Rep(j,1,k){ if(que1[j-1][0]==i-m-1) que1[j-1].pop_front(); if(que2[j-1][0]==i-m-1) que2[j-1].pop_front(); ll res1=dp[j-1][que1[j-1][0]]; ll res2=dp[j-1][que2[j-1][0]]; //cout << res1 << " " << res2 << endl; //cout << que1[j].back() << endl; dp[j][i]=max(res1+S[i],res2-S[i]); while(dp[j][que1[j].back()]-S[que1[j].back()]<=dp[j][i]-S[i]){ que1[j].pop_back(); if(que1[j].empty()) break; } que1[j].push_back(i); while(dp[j][que2[j].back()]+S[que2[j].back()]<=dp[j][i]+S[i]){ que2[j].pop_back(); if(que2[j].empty()) break; } que2[j].push_back(i); //cout << i << " " << j << " " << dp[j][i] << endl; } } ll ans=0; Rep(i,n-m,n){ ans=max(ans,dp[k-1][i]+abs(S[n]-S[i])); } cout << ans << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(50); solve(); }