#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define LEN(x) (long long)(x.size()) #define FOR(i, a, n) for(int i=(a);i<(n); ++i) #define FOE(i, a) for(auto i : a) #define ALL(c) (c).begin(), (c).end() #define RALL(c) (c).rbegin(), (c).rend() #define BIT_COUNT32(bit) (__builtin_popcount(bit)) #define BIT_COUNT64(bit) (__builtin_popcountll(bit)) template using MinPriorityQueue = std::priority_queue, std::greater >; template using MaxPriorityQueue = std::priority_queue; // @formatter:off typedef long long LL; typedef __int128_t LLL; template std::vector make_v(size_t a){return std::vector(a);} template auto make_v(size_t a, Ts... ts){ return std::vector(ts...))>(a,make_v(ts...));} // C++14 template typename std::enable_if::value==0>::type fill_v(T &t,const V &v){t=v;} template typename std::enable_if::value!=0>::type fill_v(T &t,const V &v){for(auto &e:t) fill_v(e,v);} template inline T ceil(T a, T b) { assert(a >= 0 and b > 0); return (a + b - 1) / b; } void print() { std::cout << std::endl; } template void print(Head&& head, Tail&&... tail) { std::cout << head; if (sizeof...(tail) != 0) {std::cout << " ";} print(std::forward(tail)...); } template void print(std::vector &v) {for (auto& a : v) { std::cout << a; if (&a != &v.back()) {std::cout << " ";} }std::cout << std::endl;} template void print(std::pair &p) { std::cout << p.first << " " << p.second << std::endl; } void debug() { std::cerr << std::endl; } template void debug(Head&& head, Tail&&... tail) { std::cerr << head; if (sizeof...(tail) != 0) {std::cerr << " ";} debug(std::forward(tail)...); } template void debug(std::vector &v) {for (auto& a : v) { std::cerr << a; if (&a != &v.back()) {std::cerr << " ";} }std::cerr << std::endl;} template void debug(std::pair &p) { std::cerr << p.first << " " << p.second << std::endl; } inline bool inside(long long y, long long x, long long H, long long W) {return 0 <= y and y < H and 0 <= x and x < W; } template inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } template inline T euclidean_distance2(T y1, T x1, T y2, T x2) { return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); } template inline T manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); } template T &chmin(T &a, const T &b) { return a = std::min(a, b); } template T &chmax(T &a, const T &b) { return a = std::max(a, b); } bool is_bit_on(const unsigned long long bit, const unsigned int i) { return (bit >> i) & 1u; } unsigned long long get_bit_set(const unsigned long long bit, const unsigned int i, const unsigned int b) { assert(b == 0 or b == 1); if (b == 0) { return bit & ~(1ull << i); } else {return bit | (1ull << i);}} // 初項s交差d長さnの数列の和 long long sum_of_arithmetic_progression(long long s, long long d, long long n) { return n * (2 * s + (n - 1) * d) / 2; } // 三角数 long long triangular_number(long long n) { return n * (n + 1) / 2; } // sqrt(x)の整数解を求める // 整数解がなければ-1 long long sqrt_integer(const long long x) { if (x < 0) { return -1; } auto a = (long long)sqrt(x); if (a * a == x) { return a; } if((a - 1) * (a - 1) == x) { return a - 1; } if((a + 1) * (a + 1) == x) { return a + 1; } return -1; } // xが2の階乗かどうか判定 bool is_power_of_two(long long x) { return !(x & (x - 1)); } // O(log max(a, b_sum)) long long gcd(long long a, long long b) { if (b == 0) { return a; } return gcd(b, a % b); } long long lcm(long long a, long long b) { long long g = gcd(a, b); return a / g * b; } const int INF = 1u << 30u; // 1,073,741,824 const long long LINF = 1ull << 60u; const double EPS = 1e-9; const long double PI = acos(-1.0); // 2次元配列上での移動.右,下,左,上,右上,右下,左下,左上 const std::vector dy8 = {0, 1, 0, -1, -1, 1, 1, -1}, dx8 = {1, 0, -1, 0, 1, 1, -1, -1}; // @formatter:on using namespace std; #include #include #include #include // 最大流問題を解く O(|E||V|^2) class Dinic { public: struct Edge { const unsigned int to; // 行き先のノードid long long flow; // 流量 const long long cap; // 容量 const unsigned int rev; // 逆辺のノードid const bool is_rev; // 逆辺かどうか Edge(int to, long long flow, long long cap, int rev, bool is_rev) : to(to), flow(flow), cap(cap), rev(rev), is_rev(is_rev) { assert(this->cap >= 0); } }; std::vector > graph; // グラフの隣接リスト表現 std::vector level; // sからの距離 std::vector iter; // どこまで調べ終わったか Dinic(unsigned int num_of_node) { assert(num_of_node > 0); this->graph.resize(num_of_node); this->level.resize(num_of_node); this->iter.resize(num_of_node); } // fromからtoへ向かう容量capの辺をグラフに追加する void add_edge(unsigned int from, unsigned int to, long long cap) { this->graph[from].emplace_back(Edge(to, 0, cap, (unsigned int) graph[to].size(), false)); this->graph[to].emplace_back(Edge(from, cap, cap, (unsigned int) graph[from].size() - 1, true)); } // sからtへの最大流を求める long long max_flow(unsigned int s, unsigned int t) { long long flow = 0; while (true) { this->bfs(s); if (this->level[t] < 0) { return flow; } fill(this->iter.begin(), this->iter.end(), 0); long long f; while ((f = dfs(s, t, 10000000000ll)) > 0) { flow += f; } } } private: // sからの最短距離をBFSで計算する void bfs(unsigned int s) { fill(this->level.begin(), this->level.end(), -1); std::queue que; this->level[s] = 0; que.push(s); while (not que.empty()) { unsigned int v = que.front(); que.pop(); for (int i = 0; i < (int) this->graph[v].size(); ++i) { Edge &e = this->graph[v][i]; if ((e.cap - e.flow) > 0 and level[e.to] < 0) { this->level[e.to] = this->level[v] + 1; que.push(e.to); } } } } // 増加パスをDFSで探す long long dfs(unsigned int v, unsigned int t, long long f) { if (v == t) { return f; } for (unsigned int &i = this->iter[v]; i < this->graph[v].size(); ++i) { Edge &e = this->graph[v][i]; if ((e.cap - e.flow) > 0 and this->level[v] < this->level[e.to]) { long long d = dfs(e.to, t, std::min(f, e.cap - e.flow)); if (d > 0) { e.flow += d; this->graph[e.to][e.rev].flow -= d; return d; } } } return 0; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N, M; cin >> N >> M; vector A(N); FOR(i, 0, N) { cin >> A[i]; } vector B(M); FOR(i, 0, M) { cin >> B[i]; } auto C = make_v(M, 0); FOR(i, 0, M) { int K; cin >> K; FOR(j, 0, K) { LL c; cin >> c; c--; C[i].emplace_back(c); } } Dinic dinic(N + M + 2); int s = N + M; int t = s + 1; FOR(i, 0, N) { dinic.add_edge(i, t, A[i]); } LL ans = 0; FOR(i, 0, M) { ans += B[i]; dinic.add_edge(s, N + i, B[i]); } FOR(i, 0, M) { FOR(j, 0, LEN(C[i])) { dinic.add_edge(N + i, C[i][j], INF); } } ans -= dinic.max_flow(s, t); print(ans); return 0; }