codeforces
给出n张卡,每张卡可以用无限次,每种卡需要 Ci 的花费,问最少用多少花费,能够组成所有的自然数。
#include
#include
#include
#include
#include
#define MAX 307
using namespace std;
typedef pair<int,int> PII;
typedef map<int,int> MII;
int n;
MII dp;
PII a[MAX];
int gcd ( int a , int b )
{
return !b?a:gcd ( b , a%b );
}
int main ( )
{
while ( ~scanf ("%d" , &n ) )
{
for ( int i = 0 ; i < n ; i++ )
scanf ( "%d" , &a[i].first );
for ( int i = 0 ; i < n ; i++ )
scanf ( "%d" , &a[i].second );
dp.clear();
MII::iterator it;
dp[0] = 0;
for ( int i = 0; i < n ; i++ )
{
int x = a[i].first;
int c = a[i].second;
for ( it = dp.begin() ; it != dp.end() ; it++ )
{
int y = it->first;
int d = gcd ( x , y );
int temp = it->second+c;
if ( dp[d] && dp[d] < temp ) continue;
dp[d] = temp;
}
}
if ( !dp[1] ) puts ( "-1" );
else printf ( "%d\n" , dp[1] );
}
}