Advertisement
STANAANDREY

simint 1

Oct 25th, 2021
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. ///************************************************
  5. int solve(int n, int k, const vector<int> &v)
  6. {
  7.     if (k > n) {
  8.         return 0;
  9.     }
  10.     int sum = 0, maxSum = INT_MIN;
  11.     for (int i = 0; i < k; i++)
  12.         sum += v[i];
  13.     if (sum > maxSum)
  14.     {
  15.         maxSum = sum;
  16.     }
  17.     for (int i = k; i < n; i++)
  18.     {
  19.         sum -= v[i - k];
  20.         sum += v[i];
  21.         if (sum > maxSum)
  22.         {
  23.             maxSum = sum;
  24.         }
  25.     }
  26.     int totalSum = 0;
  27.     for (int i = 0; i < n; i++)
  28.         totalSum += v[i];
  29.     if (maxSum == INT_MIN)
  30.     {
  31.         maxSum = 0;
  32.     }
  33.     return totalSum - maxSum;
  34. }
  35.  
  36. int main()
  37. {
  38.     int n, k;
  39.     cin >> n >> k;
  40.     vector<int> v(n);
  41.     for (int &nr : v) {
  42.         cin >> nr;
  43.     }
  44.     cout << solve(n, k, v) << endl;
  45.     return 0;
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement