fork(1) download
  1. // iostream is too mainstream
  2. #include <cstdio>
  3. // bitch please
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11. #include <stack>
  12. #include <list>
  13. #include <cmath>
  14. #include <iomanip>
  15. #define dibs reserve
  16. #define OVER9000 1234567890
  17. #define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
  18. #define tisic 47
  19. #define soclose 1e-6
  20. #define chocolate win
  21. // so much chocolate
  22. #define patkan 9
  23. #define ff first
  24. #define ss second
  25. #define abs(x) ((x < 0)?-(x):x)
  26. #define uint unsigned int
  27. using namespace std;
  28. // mylittledoge
  29.  
  30. int main() {
  31. cin.sync_with_stdio(0);
  32. cin.tie(0);
  33. int N;
  34. cin >> N;
  35. vector< pair<int,int> > A(N);
  36. int W =0;
  37. for(int i =0; i < N; i++) {
  38. cin >> A[i].ss >> A[i].ff;
  39. W +=A[i].ss;
  40. A[i].ff +=A[i].ss;}
  41. sort(A.begin(),A.end());
  42.  
  43. int ansA =-1000000000-tisic, ansB =W;
  44. while(ansB-ansA > 1) {
  45. int K =(ansA+ansB)/2;
  46. bool ok =true;
  47. int a =N-1, w =W;
  48. priority_queue<int> q;
  49. for(int i =N-1; i >= 0; i--) {
  50. while(a >= 0 && w-K <= A[a].ff) {
  51. q.push(A[a].ss);
  52. a--;}
  53. if(q.empty()) {ok =false; break;}
  54. w -=q.top();
  55. q.pop();}
  56. if(ok) ansB =K;
  57. else ansA =K;}
  58.  
  59. cout << ansB << "\n";
  60. return 0;}
  61.  
  62. // look at my code
  63. // my code is amazing
Runtime error #stdin #stdout #stderr 0s 4460KB
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