单调队列板子题。
很明显\[Ans=\max_{1≤i≤n} \{\sum_{j=1}^i P_i-\min_{0≤k<i} \{\sum_{j=1}^k P_j\} \}\]
单调队列维护即可。
#include <iostream>
#include <cstdio>
#include <queue>
const int max_n = 500000 + 5;
const int inf = 0x7f7f7f7f;
int N, M, Ans = -inf;
int sum[max_n];
std::deque <int> q;
inline int read()
{
register int x = 0, v = 1;
register char ch = getchar();
while(!isdigit(ch))
{
if(ch == ‘-‘) v = -1;
ch = getchar();
}
while(isdigit(ch))
{
x = (x << 1) + (x << 3) + ch - ‘0‘;
ch = getchar();
}
return x * v;
}
int main()
{
N = read();
M = read();
for(int i = 1; i <= N; ++i) sum[i] = sum[i - 1] + read();
for(int i = 1; i <= N; ++i)
{
while(!q.empty() && sum[q.back()] > sum[i]) q.pop_back();
q.push_back(i);
while(!q.empty() && i - q.front() > M) q.pop_front();
Ans = std::max(Ans, sum[i] - sum[q.front()]);
}
printf("%d\n", Ans);
return 0;
}