#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair; using pll = pair; using i128 = __int128_t; #define LINF 9223372036854775807 #define rep(i, x, limit) for (ll i = (ll)x; i < (ll)limit; i++) // Dinic's algorithm for maximum flow // Complexity: O(V^2E) for general graph, O(min(V^(2/3), E^(1/2))E) for unit template struct Dinic { const flow_t INF; struct edge { int to; flow_t cap; int rev; bool isrev; int idx; }; vector> graph; vector min_cost, iter; // V: the number of vertices explicit Dinic(int V) : INF(numeric_limits::max()), graph(V) {} // fromからtoへの容量capの辺をグラフに追加する void add_edge(int from, int to, flow_t cap, int idx = -1) { graph[from].emplace_back( (edge){to, cap, (int)graph[to].size(), false, idx}); graph[to].emplace_back( (edge){from, 0, (int)graph[from].size() - 1, true, idx}); } bool build_augment_path(int s, int t) { min_cost.assign(graph.size(), -1); queue que; min_cost[s] = 0; que.push(s); while (!que.empty() && min_cost[t] == -1) { int p = que.front(); que.pop(); for (auto &e : graph[p]) { if (e.cap > 0 && min_cost[e.to] == -1) { min_cost[e.to] = min_cost[p] + 1; que.push(e.to); } } } return min_cost[t] != -1; } flow_t find_min_dist_augment_path(int idx, const int t, flow_t flow) { if (idx == t) return flow; for (int &i = iter[idx]; i < (int)graph[idx].size(); i++) { edge &e = graph[idx][i]; if (e.cap > 0 && min_cost[idx] < min_cost[e.to]) { flow_t d = find_min_dist_augment_path(e.to, t, min(flow, e.cap)); if (d > 0) { e.cap -= d; graph[e.to][e.rev].cap += d; return d; } } } return 0; } // sからtへの最大流を求め, その値を返す flow_t max_flow(int s, int t) { flow_t flow = 0; while (build_augment_path(s, t)) { iter.assign(graph.size(), 0); flow_t f; while ((f = find_min_dist_augment_path(s, t, INF)) > 0) flow += f; } return flow; } void output() { for (int i = 0; i < graph.size(); i++) { for (auto &e : graph[i]) { if (e.isrev) continue; auto &rev_e = graph[e.to][e.rev]; cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl; } } } vector min_cut(int s) { vector used(graph.size()); queue que; que.emplace(s); used[s] = true; while (not que.empty()) { int p = que.front(); que.pop(); for (auto &e : graph[p]) { if (e.cap > 0 and not used[e.to]) { used[e.to] = true; que.emplace(e.to); cout << p << " " << e.to << endl; } } } return used; } }; int main() { ll n, m; cin >> n >> m; vector a(n), b(m); rep(i, 0, n) cin >> a[i]; rep(i, 0, m) cin >> b[i]; vector> c(m); rep(i, 0, m) { ll k; cin >> k; rep(j, 0, k) { ll x; cin >> x; x--; c[i].push_back(x); } } Dinic dinic(n + m + 2); ll s = n + m, t = n + m + 1; ll sum = 0; rep(i, 0, n) { dinic.add_edge(i, t, a[i]); sum += 0; } rep(i, 0, m) { sum -= b[i]; dinic.add_edge(s, i + n, b[i]); for (auto x : c[i]) // ボーナスiの中身 { dinic.add_edge(i + n, x, LINF); } } cout << -(sum + dinic.max_flow(s, t)) << endl; }