这是一道很容易超时的题,我超了n次了,后来队友提示我才想到,bigsum ! ! ! !就是前n项和的前n项和
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int sum[1000010],n; int bigsum[1000010]; int main() { int m; while(~scanf("%d%d",&n,&m)) { sum[0] = bigsum[0] = 0; for(int i = 1;i <= n;i++) { int nu; scanf("%d",&nu); sum[i] = sum[i-1] + nu; bigsum[i] = bigsum[i-1] + sum[i]; } long long max = 0,num = 0; for(int i = m;i <= n;i++) { num = 0; num = m * sum[i] - (bigsum[i-1] - bigsum[i-m-1]); if(num > max) max = num; } printf("%I64d ",max); } return 0; }