提交时间:2023-08-14 12:29:16

运行 ID: 98222

#include <iostream> using namespace std; const int N = 2e3 + 5; int n, K, x, a[N], dp[N][N], f[N], ans, sum[N]; signed main() { cin >> n >> K >> x; for (int i = 1; i <= n; ++ i) { cin >> a[i]; } if (n <= 5e3) { if (x < 0) { K = max(n - K, 0); x = -x; } for (int i = 1; i <= n; ++ i) { for (int j = 2; j <= n; ++ j) { dp[i][j] = -1e9; } dp[i][1] = a[i] + x; if (K == 0) { dp[i][1] -= 2 * x; } } for (int i = 1; i <= n; ++ i) { ans = max(ans, dp[i][1]); for (int j = 1; i + j - 1 <= n; ++ j) { int f = 1; if (j + 1 > K) { f = -1; } dp[i][j + 1] = dp[i][j] + a[i + j] + f * x; ans = max(ans, dp[i][j + 1]); } } cout << max(ans, 0); } }