#include using namespace std; using ll = long long; using VL = vector; #define FOR(i,a,n) for(int i=(a);i<(n);++i) #define eFOR(i,a,n) for(int i=(a);i<=(n);++i) #define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i) #define all(i) i.begin(),i.end() constexpr ll INF = 1000000000; constexpr ll LLINF = 1LL << 60; templateinline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; } inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } int main() { init(); int n, k, m; cin >> n >> k >> m; assert(1 <= n && n <= 5000); assert(1 <= k && k <= n); assert(1 <= m && m <= n); assert(n <= m * k); VL a(n + 1); FOR(i, 0, n)cin >> a[i]; assert(-INF <= *min_element(all(a)) && *max_element(all(a)) <= INF); rFOR(i, 0, n)a[i] += a[i + 1]; vector>> plus(k), minus(k); plus[0].emplace_back(-1, a[0]), minus[0].emplace_back(-1, -a[0]); vector dp(2, VL(k + 1, -LLINF)); FOR(i, 0, n) { eFOR(j, 0, k)dp[(i + 1) & 1][j] = 0; rFOR(j, 0, k) { while (!plus[j].empty() && plus[j].front().first < i - m)plus[j].pop_front(); while (!minus[j].empty() && minus[j].front().first < i - m)minus[j].pop_front(); if (!plus[j].empty())chmax(dp[(i + 1) & 1][j + 1], plus[j].front().second - a[i + 1]); if (!minus[j].empty())chmax(dp[(i + 1) & 1][j + 1], minus[j].front().second + a[i + 1]); if (j + 1 < k) { while (!plus[j + 1].empty() && plus[j + 1].back().second <= dp[(i + 1) & 1][j + 1] + a[i + 1])plus[j + 1].pop_back(); while (!minus[j + 1].empty() && minus[j + 1].back().second <= dp[(i + 1) & 1][j + 1] - a[i + 1])minus[j + 1].pop_back(); plus[j + 1].emplace_back(i, dp[(i + 1) & 1][j + 1] + a[i + 1]); minus[j + 1].emplace_back(i, dp[(i + 1) & 1][j + 1] - a[i + 1]); } } } cout << dp[n & 1][k] << "\n"; return 0; }