/* -*- coding: utf-8 -*- * * 2713.cc: No.2713 Just Solitaire - yukicoder */ #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100; const int MAX_M = 100; const long long LINF = 1LL << 60; /* typedef */ typedef long long ll; template struct MaxFlow { typedef pair pii; typedef vector vpii; typedef vector vvpii; int n, m; vvpii nbrs; T inf; vpii prvs; vector minfs, caps, flows; MaxFlow(int _n, T _inf): n(_n), m(0), inf(_inf) { nbrs.assign(n, vpii()); minfs.assign(n, 0); prvs.assign(n, pii(-1, -1)); caps.clear(), flows.clear(); } int addedges(int u, int v, T c) { // edge[m]: u -> v, cap=c nbrs[u].push_back(pii(v, m)); caps.push_back(c); flows.push_back(0); // edge[m + 1]: v -> u, cap=0 nbrs[v].push_back(pii(u, m + 1)); caps.push_back(0); flows.push_back(0); m += 2; return m; } T flow(int st, int gl) { fill(flows.begin(), flows.end(), 0); T max_flow = 0; for (;;) { //printf("max_flow = %d, limit = %d\n", max_flow, limit); fill(prvs.begin(), prvs.end(), pii(-1, -1)); prvs[st] = pii(st, -1); minfs[st] = inf; queue q; q.push(st); while (! q.empty()) { int ui = q.front(); q.pop(); if (ui == gl) break; vpii& nbru = nbrs[ui]; for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) { int vi = vit->first, ei = vit->second; T vc = caps[ei] - flows[ei]; if (prvs[vi].first < 0 && vc > 0) { prvs[vi] = pii(ui, ei); minfs[vi] = (minfs[ui] < vc) ? minfs[ui] : vc; q.push(vi); } } } if (prvs[gl].first < 0) break; T min_flow = minfs[gl]; for (int j = gl; j != st;) { int i = prvs[j].first, ei = prvs[j].second; flows[ei] += min_flow; flows[ei ^ 1] -= min_flow; j = i; } max_flow += min_flow; } return max_flow; } }; /* int main() { const int INF = 1 << 30; MaxFlow mf(3, INF); mf.addedges(0, 1, 1); mf.addedges(1, 2, 3); mf.addedges(0, 2, 1); int f = mf.flow(0, 2); printf("f=%d\n", f); return 0; } */ /* global variables */ int as[MAX_N], bs[MAX_M]; /* subroutines */ /* main */ int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", as + i); for (int i = 0; i < m; i++) scanf("%d", bs + i); int st = n + m, gl = st + 1, gn = gl + 1; MaxFlow mf(gn, LINF); for (int i = 0; i < n; i++) mf.addedges(st, i, as[i]); for (int i = 0; i < m; i++) mf.addedges(n + i, gl, bs[i]); for (int i = 0; i < m; i++) { int ki; scanf("%d", &ki); for (int j = 0; j < ki; j++) { int cj; scanf("%d", &cj); cj--; mf.addedges(cj, n + i, LINF); } } ll xf = mf.flow(st, gl); ll bsum = 0; for (int i = 0; i < m; i++) bsum += bs[i]; printf("%lld\n", bsum - xf); return 0; }