#include<bits/stdc++.h> #define N 500010 usingnamespace std;
int sum[N], a[N]; deque<int> q;
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; sum[i] = sum[i - 1] + a[i]; } int ans = -INT_MAX; for (int i = 1; i <= n; i++) { while (!q.empty() && sum[i - 1] < sum[q.front()]) q.pop_front(); q.push_front(i - 1); if (i - 1 - q.back() == m) q.pop_back(); ans = max(ans, sum[i] - sum[q.back()]); } cout << ans << endl; return0; }
int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> dp[i]; sum += dp[i]; } deque<ll> q; ll ans = INFINITY; q.push_front(0); for (int i = 1; i <= n; i++) { if (i - q.back() > k + 1) q.pop_back(); dp[i] += dp[q.back()]; while (!q.empty() && dp[i] < dp[q.front()]) q.pop_front(); q.push_front(i); } for (int i = n - k; i <= n; i++) ans = min(ans, dp[i]); cout << sum - ans << endl; return0; }
int g[N][N]; int row_min[N][N], row_max[N][N], col_min[N], col_max[N]; int tmp[N]; int a, b, n; deque<int> q;
voidgetMax(constint arr[], int dest[], int size){ // Get max segmental element from arr and copy the result to dest q.clear(); for (int i = 1; i <= size; i++) { if (!q.empty() && i - q.back() >= n) q.pop_back(); while (!q.empty() && arr[i] >= arr[q.front()]) q.pop_front(); q.push_front(i); dest[i] = arr[q.back()]; } }
voidgetMin(constint arr[], int dest[], int size){ q.clear(); for (int i = 1; i <= size; i++) { if (!q.empty() && i - q.back() >= n) q.pop_back(); while (!q.empty() && arr[i] <= arr[q.front()]) q.pop_front(); q.push_front(i); dest[i] = arr[q.back()]; } }