Advertisement
aayyk

Untitled

Dec 10th, 2021
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <queue>
  7. #include <climits>
  8. #include <stack>
  9. #include <string>
  10. #include <set>
  11. #include <cmath>
  12. #include <map>
  13. #include <unordered_map>
  14. #include <numeric>
  15. #include <random>
  16. #include <memory>
  17. #include <chrono>
  18. #include <iterator>
  19.  
  20. using namespace std;
  21. typedef long long ll;
  22. typedef long double ld;
  23. typedef pair<int, int> pii;
  24. #define mp make_pair
  25. #define pb push_back
  26. #define sz(x) int((x).size())
  27.  
  28.  
  29. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  30. const int N = 2e3 + 7;
  31. const int inf = INT_MAX / 3;
  32. const ll INF = LLONG_MAX / 3;
  33. const int MOD = 1e9 + 7;
  34.  
  35. ll n, H, dp[N][N], sumrost;
  36. struct Pupil {
  37.     ll h, l, id;
  38. } a[N];
  39.  
  40.  
  41. signed main() {
  42. #ifdef LOCAL
  43.     freopen("input.txt", "r", stdin);
  44.     freopen("output.txt", "w", stdout);
  45. #endif
  46.     cout << fixed << setprecision(9);
  47.     ios::sync_with_stdio(false);
  48.     cin.tie();
  49.     cout.tie();
  50.  
  51.     cin >> n;
  52.     for (int i = 1; i <= n; i++) {
  53.         cin >> a[i].h >> a[i].l;
  54.         a[i].id = i;
  55.         sumrost += a[i].h;
  56.     }
  57.     sort(a + 1, a + n + 1, [](Pupil a, Pupil b) { return a.h + a.l < b.h + b.l; });
  58.     cin >> H;
  59.  
  60.     for (int i = 0; i <= n; i++) {
  61.         for (int j = 1; j <= n; j++) {
  62.             dp[i][j] = -inf;
  63.         }
  64.         dp[i][0] = sumrost;
  65.     }
  66.  
  67.     for (int i = 1; i <= n; i++) {
  68.         for (int j = 1; j <= n; j++) {
  69.             dp[i][j] = dp[i - 1][j];
  70.  
  71.             if(dp[i - 1][j - 1] + a[i].l >= H && dp[i - 1][j - 1] - a[i].h > dp[i][j]) {
  72.                 dp[i][j] = dp[i - 1][j - 1] - a[i].h;
  73.             }
  74.         }
  75.     }
  76.  
  77.     ll ans = 0;
  78.     for (int j = n; j >= 0LL; j--) {
  79.         if (dp[n][j] > -inf) {
  80.             ans = j;
  81.             break;
  82.         }      
  83.     }
  84.     cout << ans << endl;
  85.     int i = n, j = ans;
  86.     while (j) {
  87.         if( dp[i - 1][j - 1] == dp[i][j] + a[i].h) {
  88.             cout << a[i].id << " ";
  89.             i--, j--;
  90.         }
  91.         else {
  92.             i--;
  93.         }
  94.     }
  95.  
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement