// V LO Kraków #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define pb push_back #define st first #define nd second int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N, K, Q; cin >> N >> K >> Q; vector<ll> a(N+1), dp(N+1); for(int i=1; i<=N; i++) { cin >> a[i]; a[i] += a[i-1]; } vector<int> result(Q); for(int q=0; q<Q; q++) { int l, r; cin >> l >> r; if(K > (r-l+1)) { result[q] = 0; continue; } for(int i=-1; i<K; i++) { dp[l+i] = 0; } const int MX = r+1; for(int i=l+K-1; i<MX; i++) { dp[i] = a[i]-a[i-K] + dp[i-K]; dp[i] = max(dp[i], dp[i-1]); } result[q] = dp[r]; } for(int i=0; i<Q; i++) { cout << result[i] << '\n'; } 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 | // V LO Kraków #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define pb push_back #define st first #define nd second int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N, K, Q; cin >> N >> K >> Q; vector<ll> a(N+1), dp(N+1); for(int i=1; i<=N; i++) { cin >> a[i]; a[i] += a[i-1]; } vector<int> result(Q); for(int q=0; q<Q; q++) { int l, r; cin >> l >> r; if(K > (r-l+1)) { result[q] = 0; continue; } for(int i=-1; i<K; i++) { dp[l+i] = 0; } const int MX = r+1; for(int i=l+K-1; i<MX; i++) { dp[i] = a[i]-a[i-K] + dp[i-K]; dp[i] = max(dp[i], dp[i-1]); } result[q] = dp[r]; } for(int i=0; i<Q; i++) { cout << result[i] << '\n'; } return 0; } |