MinhNGUYEN2k4

CATU ntu

May 15th, 2021 (edited)
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. //Nguyen Huu Hoang Minh
  2. #include <bits/stdc++.h>
  3. #define sz(x) int(x.size())
  4. #define all(x) x.begin(),x.end()
  5. #define reset(x) memset(x, 0,sizeof(x))
  6. #define pb push_back
  7. #define mp make_pair
  8. #define fi first
  9. #define se second
  10. #define N 2005
  11. #define remain(x) if (x > MOD) x -= MOD
  12. #define ii pair<int, int>
  13. #define iiii pair< ii , ii >
  14. #define viiii vector< iiii >
  15. #define vi vector<int>
  16. #define vii vector< ii >
  17. #define bit(x, i) (((x) >> (i)) & 1)
  18. #define Task "test"
  19. #define int long long
  20.  
  21. using namespace std;
  22.  
  23. typedef long double ld;
  24. const int inf = 1e10;
  25. const int minf = -1e10;
  26.  
  27. int n, w;
  28. int W[N], V[N];
  29. bool vst[N];
  30. int ans = 0;
  31. vector<int> res;
  32.  
  33. void readfile()
  34. {
  35.     ios_base::sync_with_stdio(false);
  36.     cin.tie(0);cout.tie(0);
  37.     cin >> n >> w;
  38.     for(int i=1; i<=n; i++) cin >> W[i] >> V[i];
  39. }
  40.  
  41. void ghikq()
  42. {
  43.     res.clear();
  44.     for(int i=1; i<=20; i++)
  45.     {
  46.         if (vst[i]) res.pb(i);
  47.     }
  48. }
  49.  
  50. void quaylui(int x, int res, int wei)
  51. {
  52.     if (ans < res)
  53.     {
  54.         ans = res;
  55.         ghikq();
  56.     }
  57.     if (x+1>n) return;
  58.     //chọn vật thứ i
  59.     if (W[x+1] + wei <= w){
  60.         vst[x+1] = true;
  61.         quaylui(x+1,res+V[x+1],wei+W[x+1]);
  62.         vst[x+1] = false;
  63.     }
  64.     //không chọn vật thứ i
  65.     quaylui(x+1,res,wei);
  66. }
  67.  
  68. void proc()
  69. {
  70.     if (W[1] <= w) vst[1] = true, quaylui(1,V[1],W[1]), vst[1] = false;
  71.     quaylui(1,0,0);
  72.     cout << ans << '\n';
  73.     for(auto x : res) cout << x << ' ';
  74. }
  75.  
  76. signed main()
  77. {
  78.     readfile();
  79.     proc();
  80.     return 0;
  81. }
Add Comment
Please, Sign In to add comment