#include //abs part can be fixed... long long a[3005]; long long sum[3005]; long long f[3005][3005]; struct Element{ int p; long value; Element(int p=0,long long value=0):p(p),value(value){} }; Element queue[3005][1005][2]; int h[3005][2], t[3005][2]; long long abs(long long u){ return u>0?u:-u; } long long max(long long a,long long b){ return a>b?a:b; } int main(){ int n,m,k; scanf("%d%d%d",&n,&k,&m); sum[0] = 0; for(int i = 1; i <= n; i++){ scanf("%lld",&a[i]); sum[i] = sum[i-1]+a[i]; } long long inf = 1e15+7; for(int i = 0; i <= k; i++){ f[0][i] = -inf; h[i][0] = h[i][1] = 0; t[i][0] = t[i][1] = -1; } f[0][0] = 0; queue[0][++t[0][0]][0] = Element(0,0); queue[0][++t[0][1]][1] = Element(0,0); for(int i = 1; i <= n; i++){ f[i][0] = -inf; for(int j = 1; j <= k; j++){ f[i][j] = -inf; /*for(int last = max(0,i-m); last <= i-1; last++){ //f[i][j] = max(f[i][j],f[last][j-1]+abs(sum[i]-sum[last])); //f[i][j] = max(f[i][j],f[last][j-1]+sum[i]-sum[last]); //f[i][j] = max(f[i][j],f[last][j-1]-sum[i]+sum[last]); }*/ while(h[j-1][0]<=t[j-1][0] && queue[j-1][h[j-1][0]][0].p=0 && value>=queue[j][t[j][0]][0].value) t[j][0]--; queue[j][++t[j][0]][0] = Element(i,value); value = f[i][j]+sum[i]; while(t[j][1]>=0 && value>=queue[j][t[j][1]][1].value) t[j][1]--; queue[j][++t[j][1]][1] = Element(i,value); } } printf("%lld\n",f[n][k]); return 0; }