Advertisement
a53

KSum

a53
Sep 9th, 2021
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. ifstream fin("ksum.in");
  5. ofstream fout("ksum.out");
  6. int n, k, a[100001], sum;
  7.  
  8. int main()
  9. {
  10. fin>>n>>k;
  11. for(int i=0; i<n; ++i)
  12. {
  13. fin>>a[i];
  14. if(i<k)
  15. sum+=a[i];
  16. }
  17. int maxSum[n];
  18. maxSum[0] = a[0];
  19. int curr_max = a[0];
  20. for(int i = 1; i < n; i++)
  21. {
  22. curr_max = max(a[i], curr_max+a[i]);
  23. maxSum[i] = curr_max;
  24. }
  25. int result = sum;
  26. for (int i = k; i < n; i++)
  27. {
  28. sum = sum + a[i] - a[i-k];
  29. result = max(result, sum);
  30. result = max(result, sum + maxSum[i-k]);
  31. }
  32. fout<<result;
  33. return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement