510D

1 用到了 ../../算法知识/数论/裴蜀定理

#include <bits/stdc++.h>
using namespace std;
#define int long long
map<int, int> dp;
int n, l[310], c[310];
int gcd(int a, int b)
{
    return !b ? a : gcd(b, a % b);
}
void solve()
{
    dp[0] = 0;
    map<int, int>::iterator it;
    for (int i = 1; i <= n; i++)
    {
        for (it = dp.begin(); it != dp.end(); it++)
        {
            int temp = gcd(l[i], it->first);
            if (dp.find(temp) != dp.end())
                dp[temp] = min(dp[temp], c[i] + it->second);
            else
                dp[temp] = c[i] + it->second;
        }
    }
}
signed main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> l[i];
    for (int i = 1; i <= n; i++)
        cin >> c[i];

    solve();
    if (dp.find(1) == dp.end())
    {
        cout << "-1" << endl;
        return 0;
    }
    cout << dp[1] << endl;
}