1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #include <bits/stdc++.h> #define MAX_N 300 #define MAX_M 50000 #define INF 1e16 using namespace std; typedef long long lnt; int n, s, t, cnt, pr[MAX_N+5], cr[MAX_N+5]; lnt mxf, cur; int a[MAX_N+5], f[MAX_N+5]; lnt b[MAX_N+5], c[MAX_N+5]; struct node {int v, nxt; lnt c, w;} E[MAX_M+5]; void init() {s = 0, t = n+1, memset(pr, -1, sizeof pr);} void insert(int u, int v, lnt c, lnt w) {E[cnt] = (node){v, pr[u], c, w}, pr[u] = cnt++;} void addedge(int u, int v, lnt c, lnt w) {insert(u, v, c, w), insert(v, u, 0, -w);} bool SPFA() { queue <int> que; bool inq[MAX_N+5], mrk[MAX_N+5]; lnt d[MAX_N+5]; int cr[MAX_N+5]; memset(inq, false, sizeof inq), memset(cr, -1, sizeof cr), memset(d, 0, sizeof d); memset(mrk, false, sizeof mrk), d[s] = 0, que.push(s), inq[s] = true, mrk[s] = true; while (!que.empty()) { int u = que.front(); que.pop(), inq[u] = false; for (int i = pr[u]; ~i; i = E[i].nxt) { int v = E[i].v; lnt c = E[i].c, w = E[i].w; if (c && (!mrk[v] || d[u]+w > d[v])) { mrk[v] = true, d[v] = d[u]+w, cr[v] = i; if (!inq[v]) que.push(v), inq[v] = true; } } } if (!mrk[t]) return false; lnt flow = INF; for (int i = cr[t]; ~i; i = cr[E[i^1].v]) flow = min(flow, E[i].c); for (int i = cr[t]; ~i; i = cr[E[i^1].v]) E[i].c -= flow, E[i^1].c += flow; mxf += flow, cur += d[t]*flow; if (cur < 0) {mxf -= cur%d[t] == 0 ? cur/d[t] : cur/d[t]+1; return false;} return true; } int calc(int x) { int ret = 0; for (int i = 2; i <= x; i++) while (!(x%i)) x /= i, ret++; if (x^1) ret++; return ret; } int main() { scanf("%d", &n), init(); for (int i = 1; i <= n; i++) scanf("%d", a+i), f[i] = calc(a[i]); for (int i = 1; i <= n; i++) scanf("%lld", b+i); for (int i = 1; i <= n; i++) scanf("%lld", c+i); for (int i = 1; i <= n; i++) if (f[i]&1) addedge(s, i, b[i], 0); else addedge(i, t, b[i], 0); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if ((f[i]&1) && ((f[i] == f[j]+1 && a[i]%a[j] == 0) || (f[i]+1 == f[j] && a[j]%a[i] == 0))) addedge(i, j, INF, c[i]*c[j]); while (SPFA()) ; return printf("%lld", mxf), 0; }
|