Advertisement
Kevin_Zhang

Untitled

Oct 18th, 2020
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define pb emplace_back
  3. #define ALL(i) begin(i), end(i)
  4. using namespace std;
  5. using ll = long long;
  6. #ifdef KEV
  7. #define DE(args...) cerr << "[" << #args << "] = ", kout(args)
  8. void debug(auto L, auto R) { while (L < R) cerr << *L << " \n"[L+1==R], ++L; }
  9. void kout(){ cerr << endl; }
  10. template<class T1, class ...T2> void kout(T1 a, T2 ...e) { cerr << a << ' ', kout(e...); }
  11. #else
  12. #define DE(...) 0
  13. void debug(...) {}
  14. #endif
  15. const int maxn = 16, inf = 1e9;
  16. int lim, n;
  17. int dp[1<<maxn], t[maxn], w[maxn], cost[1<<maxn];
  18.  
  19. int solve() {
  20.     for (int i = 1;i < 1<<n;++i) {
  21.         int allw = 0, allt = 0;
  22.         for (int j = 0;j < n;++j) if (i>>j&1)
  23.             allw += w[j], allt = max(allt, t[j]);
  24.  
  25.         if (allw > lim)
  26.             cost[i] = inf;
  27.         else
  28.             cost[i] = allt;
  29.     }
  30.     fill(dp+1, dp+(1<<n), inf);
  31.  
  32.     for (int i = 1;i < 1<<n;++i) {
  33.         if (i == (i&-i)) {
  34.             dp[i] = cost[i];
  35.             continue;
  36.         }
  37.         int sub = i - (i&-i);
  38.         for (int s = sub;s &= sub;--s)
  39.             dp[i] = min(dp[i], dp[s] + dp[i ^ s]);
  40. //      for (int s = i-1;s &= (i);--s)
  41. //          dp[i] = min(dp[i], dp[s] + dp[i ^ s]);
  42. //
  43.         dp[i] = min(dp[i], cost[i]);
  44.     }
  45.     return dp[(1<<n)-1];
  46. }
  47. signed main(){
  48.     ios_base::sync_with_stdio(0), cin.tie(0);
  49.     cin >> lim >> n;
  50.     for (int i = 0;i < n;++i)
  51.         cin >> t[i] >> w[i];
  52.     cout << solve() << '\n';
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement