fork download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7. using ll = long long;
  8. using ld = long double;
  9.  
  10. #define all(x) x.begin(),x.end()
  11. #define v(x) vector<x>
  12. #define nl '\n'
  13. #define fxd(x) fixed << setprecision(x)
  14. template<class t> using ordered_set = tree<t, null_type, less<t>, rb_tree_tag, tree_order_statistics_node_update>;
  15. template<class t> using ordered_multiset = tree<t, null_type, less_equal<t>, rb_tree_tag, tree_order_statistics_node_update>;
  16.  
  17.  
  18. int main()
  19. {
  20. ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  21. int n; cin >> n;
  22. multiset<ll> st;
  23.  
  24. ll total = 0;
  25. int cnt = 0;
  26. for (int i = 0; i < n; i++)
  27. {
  28.  
  29. ll x; cin >>x; st.insert(x); cnt++;
  30. total +=x;
  31. if(total < 0)
  32. {
  33. cnt--;
  34. total -= *st.begin();
  35. st.erase(st.begin());
  36. }
  37.  
  38. }
  39. cout << cnt;
  40.  
  41. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty