NOIP的时候一定要看清楚数据范围。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 100500 using namespace std; long long n,k,sum[maxn],dp[maxn],a[maxn],c[maxn],q[maxn],l=1,r=0,ans=0; int main() { scanf("%lld%lld",&n,&k); for (long long i=1;i<=n;i++) { scanf("%lld",&a[i]); sum[i]=sum[i-1]+a[i]; } c[1]=-sum[1]; for (long long i=1;i<=k;i++) { dp[i]=sum[i];ans=max(ans,dp[i]); c[i+1]=dp[i]-sum[i+1]; while ((l<=r) && (c[q[r]]<=c[i])) r--; q[++r]=i; } for (long long i=k+1;i<=n;i++) { while ((l<=r) && (q[l]<i-k)) l++; dp[i]=c[q[l]]+sum[i];ans=max(ans,dp[i]); c[i+1]=dp[i]-sum[i+1]; while ((l<=r) && (c[q[r]]<=c[i])) r--; q[++r]=i; } printf("%lld\n",ans); return 0; }