Naxocist

kusuriya

Mar 23rd, 2024
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void dbg_out() { cerr << endl; }
  5. template<typename Head, typename... Tail>
  6. void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
  7. #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
  8.  
  9. template<typename S, typename T> S amax(S &a, const T &b) { if(b > a) a = b; return a; }
  10. template<typename S, typename T> S amin(S &a, const T &b) { if(b < a) a = b; return a; }
  11.  
  12. #define all(x) x.begin(), x.end()
  13. #define allrev(x) x.rbegin(), x.rend()
  14. #define pb emplace_back
  15. #define sz(x) (int) (x).size()
  16. #define ln '\n'
  17. using ll = long long;
  18. using pi = pair<ll, ll>;
  19. using T = tuple<ll, ll, ll>;
  20. const ll INF = 2e18;
  21. int n, m;
  22.  
  23. void runcase() {
  24.     cin >> n >> m;
  25.     int T=1<<m;
  26.     vector<ll> dp(T, INF);
  27.  
  28.     for(int i=0; i<n; ++i) {
  29.         int x; cin >> x;
  30.         int mask = 0;
  31.         for(int j=0; j<m; ++j) {
  32.             int t; cin >> t; mask |= t<<j;
  33.         }
  34.         amin(dp[mask], x);
  35.     }
  36.    
  37.     for(int i=T-1; i>=0; --i) {
  38.         for(int j=0; j<m; ++j) if(i&(1<<j)) amin(dp[i^(1<<j)], dp[i]);
  39.     }
  40.  
  41.     for(int i=0; i<T; ++i) {
  42.         int t = i;
  43.         while(t) {
  44.             amin(dp[i], dp[i^t]+dp[t]);
  45.             t=(t-1)&i;
  46.         }
  47.     }
  48.  
  49.     cout << dp[T-1];
  50.     return ;
  51. }
  52.  
  53. int32_t main() {
  54.     cin.tie(nullptr)->sync_with_stdio(0);
  55.     int TC = 1;
  56.     // cin >> TC;
  57.     while(TC--) runcase();
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment