# bzoj 3158: 千钧一发

### 题解：

code：

#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<iostream>
#define LL long long
using namespace std;
int A[1010],B[1010],n;
const int inf=1<<29;
struct node{
int x,y,next,c,other;
}a[6400000];int last[100000],len=0;
int h[100000],s[100000],st,ed;
void ins(int x,int y,int c)
{
int k1=++len;
a[len].x=x;a[len].y=y;a[len].c=c;
a[len].next=last[x];last[x]=len;
int k2=++len;
a[len].x=y;a[len].y=x;a[len].c=0;
a[len].next=last[y];last[y]=len;
a[k1].other=k2;a[k2].other=k1;
}
bool bt_h()
{
memset(h,0,sizeof(h));
int l=1,r=2;s[l]=st;h[st]=1;
while(l!=r)
{
int x=s[l];
for(int i=last[x];i;i=a[i].next)
{
int y=a[i].y;
if(h[y]==0&&a[i].c>0) h[y]=h[x]+1,s[r++]=y;
}
l++;
}
return h[ed]!=0;
}
int findflow(int x,int f)
{
if(x==ed) return f;
int t,ans=0;
for(int i=last[x];i;i=a[i].next)
{
int y=a[i].y;
if(h[x]+1==h[y]&&a[i].c>0&&ans<f)
{
ans+=(t=findflow(y,min(a[i].c,f-ans)));
a[i].c-=t;a[a[i].other].c+=t;
}
}
if(ans==0) h[x]=0;
return ans;
}
LL ans=0;
int gcd(int a,int b) {return a==0?b:gcd(b%a,a);}
bool solve(LL a,LL b)
{
LL tmp=sqrt(a*a+b*b);
return tmp*tmp!=a*a+b*b;
}
bool check(int a,int b)
{
if(gcd(a,b)!=1) return true;
if(solve((LL)a,(LL)b)) return true;
return false;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&A[i]);
for(int i=1;i<=n;i++) scanf("%d",&B[i]),ans+=(LL)B[i];
st=0;ed=2*n+1;
for(int i=1;i<=n;i++) ins(st,i,B[i]),ins(i+n,ed,B[i]);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(!check(A[i],A[j])) ins(i,j+n,inf);
}
LL ANS=0;
while(bt_h()) ANS+=(LL)findflow(st,inf);
ANS/=2;
printf("%lld\n",ans-ANS);
}