#include #define rep(i,n) for (int i = 0; i < (n); i ++) using namespace std; using ll = long long; using PL = pair; using P = pair; constexpr int INF = 1000000000; constexpr long long HINF = 1000000000000000; constexpr long long MOD = 1000000007;// = 998244353; constexpr double EPS = 1e-4; constexpr double PI = 3.14159265358979; int main() { int N,K,M; cin >> N >> K >> M; vector A(N + 1); rep(i,N) cin >> A[i]; A.back() = 0; for (int i = N - 1;i >= 0;--i) A[i] += A[i + 1]; vector dp(N + 1,0); rep(i,N) dp[i + 1] = abs(A[0] - A[i + 1]); for (int i = 1;i < K;++i) { vector plus(N + 1),minus(N + 1); plus[0] = dp[0]; minus[0] = dp[0]; rep(j,N) { plus[j + 1] = dp[j + 1] + A[j + 1]; minus[j + 1] = dp[j + 1] - A[j + 1]; } deque dq; dq.push_back(0); rep(j,N) { while (!dq.empty() && dq.front() <= j - M) dq.pop_front(); if (!dq.empty()) dp[j + 1] = max(dp[j + 1],plus[dq.front()] - A[j + 1]); while (!dq.empty() && plus[dq.front()] <= plus[j + 1]) dq.pop_front(); dq.push_back(j + 1); } dq.clear(); dq.push_back(0); rep(j,N) { while (!dq.empty() && dq.front() <= j - M) dq.pop_front(); if (!dq.empty()) dp[j + 1] = max(dp[j + 1],minus[dq.front()] + A[j + 1]); while (!dq.empty() && minus[dq.front()] <= minus[j + 1]) dq.pop_front(); dq.push_back(j + 1); } } cout << dp.back() << '\n'; return 0; }