使用游标的左边和游标的右边,用两个变量记录,成功配合前缀和完成对区间的分离。

#include <bits/stdc++.h>
using namespace std;
long long a[200001];
int main() {
    int T, n, k;
    cin >> T;
    while (T--) {
        cin >> n >> k;
        for (int i = 1; i <= n; i++) cin >> a[i], a[i] += a[i - 1];
        long long maxx = -1e18, ans = -1e18;
        for (int i = k; i + k <= n; i++) {
            maxx = max(maxx, a[i] - a[i - k]);
            ans = max(ans, maxx + a[i + k] - a[i]);
        }
        cout << ans << endl;
    }
    return 0;
}