#include #include //abs part can be fixed... long long a[3005]; long long sum[3005]; long long f[3005]; struct Element{ int p; long long value; Element(int p=0,long long value=0):p(p),value(value){} }; std::dequequeue[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(){ //printf("%lld\n",sizeof(queue)/1000000); 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]; } queue[0][0].push_back(Element(0,0)); queue[0][1].push_back(Element(0,0)); for(int i = 1; i <= n; i++){ for(int j = 1; j <= k; j++){ f[j] = 0; /*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(queue[j-1][0].size()!=0 && queue[j-1][0].front().p=queue[j][0].back().value) queue[j][0].pop_back(); queue[j][0].push_back(Element(i,value)); value = f[j]+sum[i]; while(queue[j][1].size()!=0 && value>=queue[j][1].back().value) queue[j][1].pop_back(); queue[j][1].push_back(Element(i,value)); } } printf("%lld\n",f[k]); return 0; }