#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<ctime>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<map>
using namespace std;
#define MAXN 100010
#define MAXM 10000010
#define INF 1000000000
#define MOD 1000000007
#define ll long long
#define eps 1e-8
struct vec{
ll to;
ll fro;
ll v;
};
vec mp[MAXM];
ll tai[MAXN],cnt=1;
ll s,t;
ll d[MAXN];
ll q[MAXN],hd,tl;
ll ans;
ll cur[MAXN];
inline void be(ll x,ll y,ll z){
mp[++cnt].to=y;
mp[cnt].fro=tai[x];
tai[x]=cnt;
mp[cnt].v=z;
}
inline void bse(ll x,ll y,ll z){
be(x,y,z);
be(y,x,0);
}
bool bfs(){
ll i,x,y;
memset(d,0,sizeof(d));
d[s]=1;
hd=tl=0;
q[tl++]=s;
while(hd!=tl){
x=q[hd++];
for(i=tai[x];i;i=mp[i].fro){
y=mp[i].to;
if(mp[i].v&&(!d[y])){
d[y]=d[x]+1;
q[tl++]=y;
}
}
}
return d[t];
}
ll dfs(ll x,ll mx){
if(x==t){
return mx;
}
ll i,y,tt;
ll re=0;
for(i=tai[x];i;i=mp[i].fro){
y=mp[i].to;
if(d[y]==d[x]+1&&mp[i].v){
tt=dfs(y,min(mx,mp[i].v));
mp[i].v-=tt;
mp[i^1].v+=tt;
mx-=tt;
re+=tt;
if(!mx){
return re;
}
}
}
if(!re){
d[x]=0;
}
return re;
}
ll n,m;
ll a[MAXN],b[MAXN];
ll gcd(ll x,ll y){
return !y?x:gcd(y,x%y);
}
int main(){
ll i,j,x,y,k,xx,yy;
scanf("%lld",&n);
for(i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
for(i=1;i<=n;i++){
scanf("%lld",&b[i]);
ans+=b[i]*2;
}
s=n*2+1;
t=s+1;
for(i=1;i<=n;i++){
bse(s,i,b[i]);
bse(n+i,t,b[i]);
for(j=1;j<=n;j++){
if(gcd(a[i],a[j])==1&&(ll)(sqrt(a[i]*a[i]+a[j]*a[j]))*(ll)(sqrt(a[i]*a[i]+a[j]*a[j]))==a[i]*a[i]+a[j]*a[j]){
bse(i,n+j,INF);
}
}
}
while(bfs()){
ans-=dfs(s,INF);
}
printf("%lld\n",ans/2);
return 0;
}
/*
2
1 2
3 5
*/
BZOJ3158 千钧一发
最新推荐文章于 2019-02-03 14:19:00 发布