fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long int
  3. #define pb push_back
  4. #define mp make_pair
  5. #define mod 1000000007
  6. #define pi 3.141592653589793
  7. using namespace std;
  8.  
  9.  
  10. int main() {
  11. // cout << setprecision(10);
  12. ios_base::sync_with_stdio(false);
  13. cin.tie(NULL);
  14. cout.tie(NULL);
  15.  
  16. ll n, m;
  17. cin >> n >> m;
  18. pair<ll, ll> a[n];
  19. for (int i = 0; i < n; i++) {
  20. cin >> a[i].second >> a[i].first;
  21. }
  22.  
  23. ll ans = 0;
  24. sort(a, a + n, greater<pair<ll, ll>>());
  25. int d = 0;
  26.  
  27. for (auto e : a) {
  28. if (d + e.second <= m) {
  29. d++;
  30. ans += e.first;
  31. }
  32.  
  33. }
  34.  
  35.  
  36. cout << ans;
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. }
  44.  
Success #stdin #stdout 0s 4504KB
stdin
Standard input is empty
stdout
Standard output is empty