Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream fin("ksum.in");
- ofstream fout("ksum.out");
- int n, k, a[100001], sum;
- int main()
- {
- fin>>n>>k;
- for(int i=0; i<n; ++i)
- {
- fin>>a[i];
- if(i<k)
- sum+=a[i];
- }
- int maxSum[n];
- maxSum[0] = a[0];
- int curr_max = a[0];
- for(int i = 1; i < n; i++)
- {
- curr_max = max(a[i], curr_max+a[i]);
- maxSum[i] = curr_max;
- }
- int result = sum;
- for (int i = k; i < n; i++)
- {
- sum = sum + a[i] - a[i-k];
- result = max(result, sum);
- result = max(result, sum + maxSum[i-k]);
- }
- fout<<result;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement