习题-数学考试(前缀和)

习题-数学考试

#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10;

long long sum[N];

signed main(){
    int t; cin >> t;
    while(t--){
        int n, k; cin >> n >> k;
        for(int i = 1; i <= n; ++i) {cin >> sum[i]; sum[i] += sum[i-1];}
    
        long long res = -1e18, maxv = -1e18;
        for(int i = k; i + k <= n; ++i){
            maxv = max(maxv, sum[i] - sum[i-k]);
            res = max(res, maxv + sum[i+k] - sum[i]);
        }
        
        cout << res << endl;
    }

    return 0;
}
posted @ 2025-03-27 10:05  awei040519  阅读(9)  评论(0)    收藏  举报