提交时间:2023-08-14 12:24:05

运行 ID: 98158

#include <bits/stdc++.h> using namespace std; #define rep(i, s, n) for (int i = (s); i <= (n); ++i) #define all_(s, n) (s) + 1, (s) + (n) + 1 template <class T> void chmax(T& x, T y) { if (x < y) x = y; } typedef long long ll; ll n, k, x; ll a[200005], a_[200005], suma[200005]; ll f[200005], f2[200005]; ll len[200005]; int head = 1, tail = 1; ll sta[200005]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k >> x; if (x <= 0) k = n - k, x = -x; rep(i, 1, n) { cin >> a[i]; a_[i] = a[i] - x; a[i] += x; suma[i] = suma[i - 1] + a[i]; } ll ans = 0; rep(i, 1, n) { f2[i] = max(f2[i - 1] + a_[i], a_[i]); } rep(i, 1, n) { while (head != tail && suma[sta[tail - 1] - 1] >= suma[i - 1]) tail--; sta[tail++] = i; while (head != tail && i - sta[head] + 1 > k) head++; ll mn = (head == tail ? 0x3f3f3f3f3f3f3f3fll : suma[sta[head] - 1]); // cout << mn << " " << endl; int l = max(i - k, 0ll); f[i] = max(suma[i] - mn, suma[i] - suma[l] + f2[l]); // cout << f2[l] << " " << f[i] << endl; chmax(ans, f[i]); } cout << ans << endl; return 0; }