#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) { 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)) 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 // 最大流問題を解く 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; } }; template class SubmodularQUBOMinimizer { int num_variables; std::vector> unary_costs; std::map, std::array> pair_wise_costs; public: SubmodularQUBOMinimizer(): num_variables(0) { } int add_variable() { this->add_variables(1); return this->num_variables; } std::vector add_variables(const int num) { std::vector nodes(num); std::iota(nodes.begin(), nodes.end(), this->num_variables); this->num_variables += num; this->unary_costs.resize(this->num_variables); return nodes; } void add_unary_0_cost(int i, COST cost) { assert(0 <= i and i < this->num_variables); this->unary_costs[i][0b0] += cost; } void add_unary_1_cost(int i, COST cost) { assert(0 <= i and i < this->num_variables); this->unary_costs[i][0b1] += cost; } void add_pairwise_00_cost(int i, int j, COST cost) { assert(0 <= i and i < this->num_variables and 0 <= j and j < this->num_variables); if (i > j) { std::swap(i, j); } this->pair_wise_costs[{i, j}][0b00] += cost; } void add_pairwise_01_cost(int i, int j, COST cost) { assert(0 <= i and i < this->num_variables and 0 <= j and j < this->num_variables); this->pair_wise_costs[{i, j}][0b01] += cost; } void add_pairwise_10_cost(int i, int j, COST cost) { assert(0 <= i and i < this->num_variables and 0 <= j and j < this->num_variables); this->pair_wise_costs[{i, j}][0b10] += cost; } void add_pairwise_11_cost(int i, int j, COST cost) { assert(0 <= i and i < this->num_variables and 0 <= j and j < this->num_variables); if (i > j) { std::swap(i, j); } this->pair_wise_costs[{i, j}][0b11] += cost; } COST solve() { const auto offset = this->re_parameterization(); Dinic dinic(2 + num_variables); const int s = num_variables; const int t = s + 1; for (int i = 0; i < num_variables; ++i) { if (this->unary_costs[i][0b0] > 0) { dinic.add_edge(s, i, this->unary_costs[i][0]); } if (this->unary_costs[i][0b1] > 0) { dinic.add_edge(i, t, this->unary_costs[i][1]); } } for (auto &[key, cost] : this->pair_wise_costs) { auto [i, j] = key; if (cost[0b10] > 0) { dinic.add_edge(i, j, cost[0b10]); } if (cost[0b01] > 0) { dinic.add_edge(j, i, cost[0b01]); } } return dinic.max_flow(s, t) + offset; } private: COST re_parameterization() { COST offset = 0; for (auto &[key, cost] : this->pair_wise_costs) { const auto [i, j] = key; { const auto delta = std::min(cost[0b00], std::min(cost[0b01], std::min(cost[0b10], cost[0b11]))); cost[0b00] -= delta; cost[0b01] -= delta; cost[0b10] -= delta; cost[0b11] -= delta; offset += delta; } for (int b = 0; b <= 1; ++b) { const auto delta = std::min(cost[0b00 | b], cost[0b10 | b]); cost[0b00 | b] -= delta; cost[0b10 | b] -= delta; this->unary_costs[j][b] -= delta; } for (int a = 0; a <= 1; ++a) { const auto delta = std::min(cost[a << 1 | 0b00], cost[a << 1 | 0b01]); cost[a << 1 | 0b00] -= delta; cost[a << 1 | 0b01] -= delta; this->unary_costs[j][a] -= delta; } } for (int i = 0; i < this->num_variables; ++i) { const auto delta = std::min(this->unary_costs[i][0], this->unary_costs[i][1]); this->unary_costs[i][0b0] -= delta; this->unary_costs[i][0b1] -= delta; offset += delta; } return offset; } }; using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N, M; cin >> N >> M; SubmodularQUBOMinimizer solver; auto xs = solver.add_variables(N); auto ys = solver.add_variables(M); FOR(i, 0, N) { LL A; cin >> A; solver.add_unary_1_cost(xs[i], A); } FOR(j, 0, M) { LL B; cin >> B; solver.add_unary_1_cost(ys[j], -B); } FOR(j, 0, M) { int K; cin >> K; FOR(_, 0, K) { int C; cin >> C; C--; solver.add_pairwise_01_cost(xs[C], ys[j], INF); } } print(-solver.solve()); return 0; }