1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| #include <cstdio> #include <vector> #include <deque>
std::deque<std::pair<int, int> > dq; int push_back(int id, int x, int m) { while (!dq.empty() && dq.back().second <= x) dq.pop_back(); dq.push_back({id, x}); while (!dq.empty() && dq.front().first <= id - m) dq.pop_front(); return dq.front().second; } std::vector<int> monotony_queue(std::vector<int> v, int m) { dq.clear(); std::vector<int> ans;
for (int i = 0; i < m - 1; i++) push_back(i, v[i], m); for (int i = m - 1; i < v.size(); i++) { ans.push_back(push_back(i, v[i], m)); } return ans; }
#define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b))
int main() { static std::vector<int> v, s, u; int n, m, A, x;
scanf("%d%d%d", &n, &m, &A); for (int i = 0; i < n; i++) { scanf("%d", &x); v.push_back(x); }
s = monotony_queue(v, m); u = monotony_queue(v, m - 1);
u[0] = max(u[0], A); for (int i = 1; i < u.size(); i++) { u[i] = max(u[i], A); u[i] += u[i - 1]; } u.insert(u.begin(), 0);
for (int i = 1; i < s.size(); i++) { s[i] += s[i - 1]; } s.insert(s.begin(), 0);
int maxn = -998244353; for (int k = 1; k <= n; k++) { int l = max(0, k - m + 1); int r = min(n - m + 1, k); maxn = max(maxn, u[r + 1] - u[l] - s[r] + s[l]); } maxn += *s.rbegin(); printf("%d\n", maxn); return 0; }
|