fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define all(x) x.begin() , x.end()
  5. #define ll long long
  6.  
  7. void solve() {
  8. ll n , x;
  9. cin >> n >> x;
  10. vector<int>v(n);
  11. for(auto &i:v)cin >> i;
  12. vector<int>temp;
  13. ll sum = 0;
  14. sort(all(v));
  15. for (int i = 0; i < n; ++i) {
  16. if(sum + v[i] > x)break;
  17. else {
  18. sum += v[i];
  19. temp.push_back(v[i]);
  20. }
  21. }
  22. ll ans = (temp.empty() ? 0 : (ll)temp.size()) ,cnt = 0;
  23. while(!temp.empty()){
  24. ll sz = (ll)temp.size();
  25. ll c = (x-sum)/sz;
  26. cnt += c;
  27. ans += c*sz;
  28. sum += (sz*c);
  29. sum -= (temp.back()+cnt);
  30. temp.pop_back();
  31. }
  32. cout << ans;
  33. }
  34.  
  35.  
  36. int32_t main() {
  37. ios_base::sync_with_stdio(false);
  38. cin.tie(nullptr), cout.tie(nullptr);
  39. #ifndef ONLINE_JUDGE
  40. freopen("input.txt", "r", stdin);
  41. freopen("output.txt", "w", stdout);
  42. #endif
  43. int T = 1;
  44. cin >> T;
  45. while (T--) {
  46. solve();
  47. cout << '\n';
  48. }
  49. }
Runtime error #stdin #stdout #stderr 0.01s 5292KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc