提交时间:2023-08-14 13:06:22
运行 ID: 98247
#include <iostream> #define int long long 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, (long long)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, (long long)0); } }