Advertisement
smatskevich

Lesson7

Nov 14th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5. using ll=long long;
  6.  
  7. void Solve() {
  8.   int n; cin >> n;
  9.   vector<int> a(n);
  10.   for (auto& x : a) cin >> x;
  11.   int k; cin >> k;
  12.   ll mx = INT_MIN;
  13.   ll sm = 0;
  14.   for (int i = 0; i < k; ++i) sm += a[i];
  15.   for (int i = 0; i <= n - k; ++i) {
  16.     mx = max(mx, sm);
  17.     if (i < n - k) sm += a[i + k] - a[i];
  18.   }
  19.   cout << mx << "\n";
  20. }
  21. int main() {
  22.   ios::sync_with_stdio(false);
  23.   cin.tie(nullptr);
  24.   cout.tie(nullptr);
  25.  
  26.   Solve();
  27.   return 0;
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement