Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. //#define int int64_t
  3. #define all(x) (x).begin(), (x).end()
  4. #define OUT(x) ((cout << x), exit(0))
  5. #define out(x) return void(cout << x << endl)
  6. using namespace std;
  7. typedef long double db;
  8. const int64_t INF = (int64_t)(2e18);
  9. const int inf = (int)(1e9 + 7);
  10. //------------------------------------------//
  11.  
  12. inline int pw2(int n) { return (1 << n); }
  13.  
  14. inline bool cmp(int a, int b)
  15. {
  16. return __builtin_popcount(a) < __builtin_popcount(b);
  17. }
  18.  
  19.  
  20. int32_t main()
  21. {
  22. ios_base::sync_with_stdio(false);
  23. cout << fixed << setprecision(10);
  24. cin.tie(nullptr);
  25.  
  26. #ifdef _MY
  27. freopen("input.txt", "r", stdin);
  28. freopen("output.txt", "w", stdout);
  29. #endif
  30.  
  31. int n;
  32. cin >> n;
  33. vector<int64_t> a(n), b(n);
  34. for (auto& it : a) cin >> it;
  35. for (auto& it : b) cin >> it;
  36.  
  37. vector<int> order(pw2(n));
  38. iota(order.begin(), order.end(), 0);
  39. sort(order.begin(), order.end(), cmp);
  40.  
  41. vector<int64_t> dp(pw2(n), 0);
  42. for (auto& mask : order)
  43. {
  44. int ind = __builtin_popcount(mask);
  45. for (int bit = 0; bit < n; ++bit)
  46. {
  47. if ((mask >> bit) & 1) continue;
  48. int newmask = mask | (1 << bit);
  49. int64_t curres = __gcd(a[bit], b[ind]);
  50. dp[newmask] = max(dp[newmask], dp[mask] + curres);
  51. }
  52. }
  53.  
  54. cout << dp.back();
  55.  
  56.  
  57.  
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement