#include <cstdio> #include <algorithm> using namespace std; int a[300010]; long long pref[300010]; long long dp[300010]; void licz(int l, int r, int k) { dp[l - 1] = 0; if (r - l < k - 1) { printf("0\n"); return; } for (int i = l; i < l + k - 1; i++) { dp[i] = 0; } for (int i = l + k - 1; i <= r; i++) { for (int j = i; j > i - k; j--) { if (j - k < l - 1) break; long long r = max(0LL, pref[j] - pref[j - k]); dp[i] = max(dp[i], dp[j - k] + r); } } printf("%lld\n", dp[r]); for (int i = l + k - 1; i <= r; i++) { dp[i] = 0; } } int main() { int n, k, q; scanf("%d %d %d", &n, &k, &q); pref[0] = 0; for (int i = 1; i <= n; i++) { scanf("%d", a + i); pref[i] = pref[i - 1] + a[i]; } for (int i = 1; i <= q; i++) { int l, r; scanf("%d %d", &l, &r); licz(l, r, k); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | #include <cstdio> #include <algorithm> using namespace std; int a[300010]; long long pref[300010]; long long dp[300010]; void licz(int l, int r, int k) { dp[l - 1] = 0; if (r - l < k - 1) { printf("0\n"); return; } for (int i = l; i < l + k - 1; i++) { dp[i] = 0; } for (int i = l + k - 1; i <= r; i++) { for (int j = i; j > i - k; j--) { if (j - k < l - 1) break; long long r = max(0LL, pref[j] - pref[j - k]); dp[i] = max(dp[i], dp[j - k] + r); } } printf("%lld\n", dp[r]); for (int i = l + k - 1; i <= r; i++) { dp[i] = 0; } } int main() { int n, k, q; scanf("%d %d %d", &n, &k, &q); pref[0] = 0; for (int i = 1; i <= n; i++) { scanf("%d", a + i); pref[i] = pref[i - 1] + a[i]; } for (int i = 1; i <= q; i++) { int l, r; scanf("%d %d", &l, &r); licz(l, r, k); } return 0; } |