数学考试

https://ac.nowcoder.com/acm/problem/15553

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+5;
int t,n,k,s[maxn],maxpre;
int ans;
signed main(){
    //freopen("in","r",stdin);
    ios::sync_with_stdio(0);
    cin >> t;
    while(t--){
        cin >> n >> k;
        for(int i = 1; i <= n; i++){
            cin >> s[i];
            s[i] += s[i - 1];
        }
        maxpre = ans = -1e10;
        for(int i = k; i + k <= n; i++){
            maxpre = max(maxpre,s[i] - s[i - k]);
            ans = max(ans,maxpre + s[i + k] - s[i]);
        }
        cout << ans << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xcfxcf/p/12930969.html