fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define fr first
  4. #define sc second
  5. #define sz(x) (int) x.size()
  6. #define pb push_back
  7. #define all(x) x.begin(), x.end()
  8.  
  9. using namespace std;
  10. using pii = pair<ll, ll>;
  11. using tii = tuple<ll, ll, ll>;
  12. string printBin(ll m) { string a = ""; for(int i=63; i>=0; i--) { if(m & (1<<i)) {a += '1';} else {a += '0';} } return a; }
  13.  
  14. int main() {
  15. ll n, b;
  16. while(cin >> n >> b) {
  17. vector<ll> a(n+1), pref(n+1, 0);
  18.  
  19. for(int i=1; i<=n; i++) cin >> a[i], pref[i] = pref[i-1] + a[i];
  20.  
  21. priority_queue<pii> maior;
  22. priority_queue<pii, vector<pii>, greater<>> menor;
  23.  
  24. ll ans = 0;
  25. for(int i=1; i<=b; i++) {
  26. maior.push({a[i], i});
  27. menor.push({a[i], i});
  28. }
  29.  
  30. ans += pref[b] - menor.top().fr - maior.top().fr;
  31.  
  32. for(int i=b+1; i<=n; i++) {
  33. maior.push({a[i], i});
  34. menor.push({a[i], i});
  35.  
  36. ans += pref[i] - pref[i-b];
  37.  
  38. while(maior.top().sc <= i-b) maior.pop();
  39. ans -= maior.top().fr;
  40.  
  41. while(menor.top().sc <= i-b) menor.pop();
  42. ans -= menor.top().fr;
  43. }
  44.  
  45. cout << ans << endl;
  46. }
  47. }
Success #stdin #stdout 0s 544KB
stdin
Standard input is empty
stdout
Standard output is empty