提交时间:2023-08-14 12:27:01

运行 ID: 98192

#include<bits/stdc++.h> using namespace std; #ifdef IAKIOI #define cin fin ifstream cin("in.txt"); #endif #define ll long long using pii = pair<ll,int>; constexpr int N = 2e5 + 5; ll a[N], s[N], mn[N]; deque<pii>q; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; ll ans = 0, x; cin >> n >> k >> x; if (x < 0) x = -x, k = n - k; for (int i = 1; i <= n; ++i) { cin >> a[i]; a[i] -= x; s[i] = s[i - 1] + a[i]; mn[i] = min(mn[i - 1], s[i]); } x *= 2; q.emplace_back(0, 0); for (int i = 1; i <= n; ++i) { if (i >= k) ans = max(ans, s[i] - mn[i - k] + k * x); if (i - q.front().second + 1 > k) q.pop_front(); if (!q.empty()) ans = max(ans, s[i] + i * x - q.front().first); while (!q.empty() && s[i] + i * x <= q.back().first) { q.pop_back(); } q.emplace_back(s[i] + i * x, i); } cout << ans << endl; return 0; }