// なんやかんやでO(N^2.5)になってくれないか #include #include using namespace std; typedef long long ll; int main(){ int n, k, m; cin >> n >> k >> m; vector v(n+1, 0); for(int i = 1; i <= n; i++) cin >> v[i], v[i] += v[i-1]; vector> dp(n+1, vector(k+1, -1)); dp[0][0] = 0; for(int i = 1; i <= n; i++){ for(int j = (i+m-1)/m; j <= min(i, k-(n-i+m-1)/m); j++){ for(int l = max(0, i-m); l < i; l++){ // 重い? if(dp[l][j-1] != -1) dp[i][j] = max(dp[i][j], dp[l][j-1]+abs(v[i]-v[l])); } } } cout << dp[n][k] << endl; return 0; }