Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ///************************************************
- int solve(int n, int k, const vector<int> &v)
- {
- if (k > n) {
- return 0;
- }
- int sum = 0, maxSum = INT_MIN;
- for (int i = 0; i < k; i++)
- sum += v[i];
- if (sum > maxSum)
- {
- maxSum = sum;
- }
- for (int i = k; i < n; i++)
- {
- sum -= v[i - k];
- sum += v[i];
- if (sum > maxSum)
- {
- maxSum = sum;
- }
- }
- int totalSum = 0;
- for (int i = 0; i < n; i++)
- totalSum += v[i];
- if (maxSum == INT_MIN)
- {
- maxSum = 0;
- }
- return totalSum - maxSum;
- }
- int main()
- {
- int n, k;
- cin >> n >> k;
- vector<int> v(n);
- for (int &nr : v) {
- cin >> nr;
- }
- cout << solve(n, k, v) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement