#include #pragma GCC optimize("O3") using namespace std; using ll = long long; #define rep(i,n) for (int i = 0; i < (n); ++i) #define rep2(i,m,n) for (int i = (m); i < (n); ++i) #define rep3(i,a,b) for (int i = (a); i >= (b); --i) #define all(x) (x).begin(),(x).end() inline int popcount(const int x) { return __builtin_popcount(x);} inline ll popcount(const ll x) { return __builtin_popcountll(x);} template void chmin(T &a, const T &b) noexcept { if (b < a) a = b;} template void chmax(T &a, const T &b) noexcept { if (a < b) a = b;} template void drop(const T &x) { std::cout< void debug_out(const T &x, const Args &... args) { std::cout< struct queue_aggression { private: // using F = function; struct node { T val, sum; node(const T &val, const T &sum):val(val),sum(sum){} }; const F f; stack front_stack, back_stack; public: queue_aggression(const F &f):f(f){} bool empty() const { return front_stack.empty() && back_stack.empty();} int size() const { return front_stack.size() + back_stack.size();} T fold_all() { assert(!empty()); if (front_stack.empty()) return back_stack.top().sum; else if (back_stack.empty()) return front_stack.top().sum; else return f(front_stack.top().sum, back_stack.top().sum); } void push(const T &x) { if (back_stack.empty()) back_stack.emplace(x, x); else { T s = f(back_stack.top().sum, x); back_stack.emplace(x, s); } } void pop() { assert(!empty()); if (front_stack.empty()) { front_stack.emplace(back_stack.top().val, back_stack.top().val); back_stack.pop(); while(!back_stack.empty()) { T s = f(back_stack.top().val, front_stack.top().sum); front_stack.emplace(back_stack.top().val, s); back_stack.pop(); } } front_stack.pop(); } }; const ll INF = 1LL<<60; int main() { int n, k, m; cin >> n >> k >> m; vector a(n), s(n+1, 0); rep(i,n) { cin >> a[i]; s[i+1] += s[i] + a[i]; } vector > dp(n+1, vector(k+1, -INF)); dp[0][0] = 0; auto f = [](ll x, ll y){ return max(x,y);}; for (int j = 1; j <= k; ++j) { queue_aggression que1(f), que2(f); rep(i, n) { if (i/m+1 > j) break; // for (int l = max(0, i+1-m); l <= i; ++l) { //区間の始点 // // chmax(dp[i+1][j], dp[l][j-1] + (s[i+1] - s[l])); // // chmax(dp[i+1][j], dp[l][j-1] - (s[i+1] - s[l])); // chmax(dp[i+1][j], dp[l][j-1] - s[l] + s[i+1]); // chmax(dp[i+1][j], dp[l][j-1] + s[l] - s[i+1]); // } que1.push(dp[i][j-1] - s[i]); que2.push(dp[i][j-1] + s[i]); chmax(dp[i+1][j], que1.fold_all() + s[i+1]); chmax(dp[i+1][j], que2.fold_all() - s[i+1]); if (que1.size() == m) { que1.pop(); que2.pop(); } } } cout << dp[n][k] << endl; return 0; }