fork download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace __gnu_pbds;
  5. using namespace std;
  6. typedef long long ll;
  7. typedef pair<int, int> pii;
  8. typedef pair<ll, ll> pll;
  9. void IOS() { ios::sync_with_stdio(0);cin.tie(0); }
  10.  
  11. typedef tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
  12.  
  13. ordered_set tr;
  14.  
  15. int main() {
  16. IOS();
  17. int n,k,a;
  18. cin>>n>>k;
  19. vector<pii>v;
  20. for(int i=0;i<n;i++){
  21. cin>>a;
  22. v.push_back({a,i});
  23. }
  24. for(int i=0;i<k-1;i++){
  25. tr.insert(v[i]);
  26. }
  27. ll ans = 0;
  28. for(int i=k-1;i<n;i++){
  29. tr.insert(v[i]);
  30. ans+=(*(tr.find_by_order((k-1)/2))).first;
  31. tr.erase(tr.find(v[i-k+1]));
  32. }
  33. cout<<ans<<'\n';
  34. }
Success #stdin #stdout 0s 4556KB
stdin
22 7
4 3 9 5 3 6 0 2 8 4 7 9 3 8 5 7 5 9 8 3 1 1 
stdout
89