Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- //#define int int64_t
- #define all(x) (x).begin(), (x).end()
- #define OUT(x) ((cout << x), exit(0))
- #define out(x) return void(cout << x << endl)
- using namespace std;
- typedef long double db;
- const int64_t INF = (int64_t)(2e18);
- const int inf = (int)(1e9 + 7);
- //------------------------------------------//
- inline int pw2(int n) { return (1 << n); }
- inline bool cmp(int a, int b)
- {
- return __builtin_popcount(a) < __builtin_popcount(b);
- }
- int32_t main()
- {
- ios_base::sync_with_stdio(false);
- cout << fixed << setprecision(10);
- cin.tie(nullptr);
- #ifdef _MY
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- int n;
- cin >> n;
- vector<int64_t> a(n), b(n);
- for (auto& it : a) cin >> it;
- for (auto& it : b) cin >> it;
- vector<int> order(pw2(n));
- iota(order.begin(), order.end(), 0);
- sort(order.begin(), order.end(), cmp);
- vector<int64_t> dp(pw2(n), 0);
- for (auto& mask : order)
- {
- int ind = __builtin_popcount(mask);
- for (int bit = 0; bit < n; ++bit)
- {
- if ((mask >> bit) & 1) continue;
- int newmask = mask | (1 << bit);
- int64_t curres = __gcd(a[bit], b[ind]);
- dp[newmask] = max(dp[newmask], dp[mask] + curres);
- }
- }
- cout << dp.back();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement