#line 2 "/Users/noya2/Desktop/Noya2_library/template/template.hpp" using namespace std; #include #line 1 "/Users/noya2/Desktop/Noya2_library/template/inout_old.hpp" namespace noya2 { template ostream &operator<<(ostream &os, const pair &p){ os << p.first << " " << p.second; return os; } template istream &operator>>(istream &is, pair &p){ is >> p.first >> p.second; return is; } template ostream &operator<<(ostream &os, const vector &v){ int s = (int)v.size(); for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i]; return os; } template istream &operator>>(istream &is, vector &v){ for (auto &x : v) is >> x; return is; } void in() {} template void in(T &t, U &...u){ cin >> t; in(u...); } void out() { cout << "\n"; } template void out(const T &t, const U &...u){ cout << t; if (sizeof...(u)) cout << sep; out(u...); } template void out(const vector> &vv){ int s = (int)vv.size(); for (int i = 0; i < s; i++) out(vv[i]); } struct IoSetup { IoSetup(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(7); } } iosetup_noya2; } // namespace noya2 #line 1 "/Users/noya2/Desktop/Noya2_library/template/const.hpp" namespace noya2{ const int iinf = 1'000'000'007; const long long linf = 2'000'000'000'000'000'000LL; const long long mod998 = 998244353; const long long mod107 = 1000000007; const long double pi = 3.14159265358979323; const vector dx = {0,1,0,-1,1,1,-1,-1}; const vector dy = {1,0,-1,0,1,-1,-1,1}; const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const string alp = "abcdefghijklmnopqrstuvwxyz"; const string NUM = "0123456789"; void yes(){ cout << "Yes\n"; } void no(){ cout << "No\n"; } void YES(){ cout << "YES\n"; } void NO(){ cout << "NO\n"; } void yn(bool t){ t ? yes() : no(); } void YN(bool t){ t ? YES() : NO(); } } // namespace noya2 #line 1 "/Users/noya2/Desktop/Noya2_library/template/utils.hpp" namespace noya2{ unsigned long long inner_binary_gcd(unsigned long long a, unsigned long long b){ if (a == 0 || b == 0) return a + b; int n = __builtin_ctzll(a); a >>= n; int m = __builtin_ctzll(b); b >>= m; while (a != b) { int mm = __builtin_ctzll(a - b); bool f = a > b; unsigned long long c = f ? a : b; b = f ? b : a; a = (c - b) >> mm; } return a << min(n, m); } template T gcd_fast(T a, T b){ return static_cast(inner_binary_gcd(abs(a),abs(b))); } long long sqrt_fast(long long n) { if (n <= 0) return 0; long long x = sqrt(n); while ((x + 1) * (x + 1) <= n) x++; while (x * x > n) x--; return x; } template T floor_div(const T n, const T d) { assert(d != 0); return n / d - static_cast((n ^ d) < 0 && n % d != 0); } template T ceil_div(const T n, const T d) { assert(d != 0); return n / d + static_cast((n ^ d) >= 0 && n % d != 0); } template void uniq(vector &v){ sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); } template inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; } template inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; } template inline bool range(T l, T x, T r){ return l <= x && x < r; } } // namespace noya2 #line 8 "/Users/noya2/Desktop/Noya2_library/template/template.hpp" #define rep(i,n) for (int i = 0; i < (int)(n); i++) #define repp(i,m,n) for (int i = (m); i < (int)(n); i++) #define reb(i,n) for (int i = (int)(n-1); i >= 0; i--) #define all(v) (v).begin(),(v).end() using ll = long long; using ld = long double; using uint = unsigned int; using ull = unsigned long long; using pii = pair; using pll = pair; using pil = pair; using pli = pair; namespace noya2{ /* ~ (. _________ . /) */ } using namespace noya2; #line 2 "c.cpp" #line 2 "/Users/noya2/Desktop/Noya2_library/flow/project_selection.hpp" #line 2 "/Users/noya2/Desktop/Noya2_library/flow/maxflow.hpp" #line 2 "/Users/noya2/Desktop/Noya2_library/data_structure/simple_queue.hpp" #line 4 "/Users/noya2/Desktop/Noya2_library/data_structure/simple_queue.hpp" namespace noya2::internal { template struct simple_queue { std::vector payload; int pos = 0; void reserve(int n) { payload.reserve(n); } int size() const { return int(payload.size()) - pos; } bool empty() const { return pos == int(payload.size()); } void push(const T& t) { payload.push_back(t); } T& front() { return payload[pos]; } void clear() { payload.clear(); pos = 0; } void pop() { pos++; } }; } // namespace noya2::internal #line 4 "/Users/noya2/Desktop/Noya2_library/flow/maxflow.hpp" #line 10 "/Users/noya2/Desktop/Noya2_library/flow/maxflow.hpp" namespace noya2 { template struct mf_graph { public: mf_graph() : _n(0) {} explicit mf_graph(int n) : _n(n), g(n) {} int add_edge(int from, int to, Cap cap) { assert(0 <= from && from < _n); assert(0 <= to && to < _n); assert(0 <= cap); int m = int(pos.size()); pos.push_back({from, int(g[from].size())}); int from_id = int(g[from].size()); int to_id = int(g[to].size()); if (from == to) to_id++; g[from].push_back(_edge{to, to_id, cap}); g[to].push_back(_edge{from, from_id, 0}); return m; } struct edge { int from, to; Cap cap, flow; }; edge get_edge(int i) { int m = int(pos.size()); assert(0 <= i && i < m); auto _e = g[pos[i].first][pos[i].second]; auto _re = g[_e.to][_e.rev]; return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap}; } std::vector edges() { int m = int(pos.size()); std::vector result; for (int i = 0; i < m; i++) { result.push_back(get_edge(i)); } return result; } void change_edge(int i, Cap new_cap, Cap new_flow) { int m = int(pos.size()); assert(0 <= i && i < m); assert(0 <= new_flow && new_flow <= new_cap); auto& _e = g[pos[i].first][pos[i].second]; auto& _re = g[_e.to][_e.rev]; _e.cap = new_cap - new_flow; _re.cap = new_flow; } Cap flow(int s, int t) { return flow(s, t, std::numeric_limits::max()); } Cap flow(int s, int t, Cap flow_limit) { assert(0 <= s && s < _n); assert(0 <= t && t < _n); assert(s != t); std::vector level(_n), iter(_n); internal::simple_queue que; auto bfs = [&]() { std::fill(level.begin(), level.end(), -1); level[s] = 0; que.clear(); que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (auto e : g[v]) { if (e.cap == 0 || level[e.to] >= 0) continue; level[e.to] = level[v] + 1; if (e.to == t) return; que.push(e.to); } } }; auto dfs = [&](auto self, int v, Cap up) { if (v == s) return up; Cap res = 0; int level_v = level[v]; for (int& i = iter[v]; i < int(g[v].size()); i++) { _edge& e = g[v][i]; if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue; Cap d = self(self, e.to, std::min(up - res, g[e.to][e.rev].cap)); if (d <= 0) continue; g[v][i].cap += d; g[e.to][e.rev].cap -= d; res += d; if (res == up) return res; } level[v] = _n; return res; }; Cap flow = 0; while (flow < flow_limit) { bfs(); if (level[t] == -1) break; std::fill(iter.begin(), iter.end(), 0); Cap f = dfs(dfs, t, flow_limit - flow); if (!f) break; flow += f; } return flow; } std::vector min_cut(int s) { std::vector visited(_n); internal::simple_queue que; que.push(s); while (!que.empty()) { int p = que.front(); que.pop(); visited[p] = true; for (auto e : g[p]) { if (e.cap && !visited[e.to]) { visited[e.to] = true; que.push(e.to); } } } return visited; } private: int _n; struct _edge { int to, rev; Cap cap; }; std::vector> pos; std::vector> g; }; } // namespace noya2 #line 2 "/Users/noya2/Desktop/Noya2_library/data_structure/csr.hpp" #line 4 "/Users/noya2/Desktop/Noya2_library/data_structure/csr.hpp" #include #line 7 "/Users/noya2/Desktop/Noya2_library/data_structure/csr.hpp" namespace noya2::internal { template struct csr final { csr () {} csr (int _n) : n(_n) {} csr (int _n, int m) : n(_n){ start.reserve(m); elist.reserve(m); } // ACL style constructor (do not have to call build) csr (int _n, const std::vector> &idx_elem) : n(_n), start(_n + 2), elist(idx_elem.size()) { for (auto &[i, e] : idx_elem){ start[i + 2]++; } for (int i = 1; i < n; i++){ start[i + 2] += start[i + 1]; } for (auto &[i, e] : idx_elem){ elist[start[i + 1]++] = e; } prepared = true; } int add(int idx, E elem){ int eid = start.size(); start.emplace_back(idx); elist.emplace_back(elem); return eid; } void build(){ if (prepared) return ; int m = start.size(); std::vector nelist(m); std::vector nstart(n + 2, 0); for (int i = 0; i < m; i++){ nstart[start[i] + 2]++; } for (int i = 1; i < n; i++){ nstart[i + 2] += nstart[i + 1]; } for (int i = 0; i < m; i++){ nelist[nstart[start[i] + 1]++] = elist[i]; } swap(elist,nelist); swap(start,nstart); prepared = true; } const auto operator[](int idx) const { return std::ranges::subrange(elist.begin()+start[idx],elist.begin()+start[idx+1]); } auto operator[](int idx){ return std::ranges::subrange(elist.begin()+start[idx],elist.begin()+start[idx+1]); } const auto operator()(int idx, int l, int r) const { return std::ranges::subrange(elist.begin()+start[idx]+l,elist.begin()+start[idx]+r); } auto operator()(int idx, int l, int r){ return std::ranges::subrange(elist.begin()+start[idx]+l,elist.begin()+start[idx]+r); } int n; std::vector start; std::vector elist; bool prepared = false; }; } // namespace noya2::internal #line 151 "/Users/noya2/Desktop/Noya2_library/flow/maxflow.hpp" namespace noya2 { template struct maxflow { struct edge { int from, to; Cap cap, flow; }; maxflow() : n(0) {} explicit maxflow(int _n) : n(_n), g(n), degree(_n,0) {} int add_edge(int from, int to, Cap cap){ assert(0 <= from && from < n); assert(0 <= to && to < n); assert(0 <= cap); int eid = (int)(pos.size()); int from_id = degree[from]; int to_id = degree[to] + (from == to ? 1 : 0); g.add(from, _edge{to, to_id, cap}); degree[from]++; g.add(to, _edge{from, from_id, 0}); degree[to]++; pos.emplace_back(from, from_id); return eid; } edge get_edge(int i) { auto e = g[pos[i].first][pos[i].second]; auto re = g[e.to][e.rev]; return edge{pos[i].first, e.to, e.cap + re.cap, re.cap}; } Cap flow(int s, int t){ return flow(s, t, std::numeric_limits::max()); } Cap flow(int s, int t, Cap flow_limit){ assert(0 <= s && s < n); assert(0 <= t && t < n); assert(s != t); g.build(); std::vector level(n), iter(n); internal::simple_queue que; auto bfs = [&]() { std::fill(level.begin(), level.end(), -1); level[s] = 0; que.clear(); que.push(s); while (!que.empty()){ int v = que.front(); que.pop(); for (auto e : g[v]){ if (e.cap == 0 || level[e.to] >= 0) continue; level[e.to] = level[v] + 1; if (e.to == t) return ; que.push(e.to); } } }; auto dfs = [&](auto sfs, int v, Cap up){ if (v == s) return up; Cap res = 0; int level_v = level[v]; for (int &i = iter[v]; i < degree[v]; i++){ _edge &e = g[v][i]; if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue; Cap d = sfs(sfs, e.to, std::min(up - res, g[e.to][e.rev].cap)); if (d <= 0) continue; g[v][i].cap += d; g[e.to][e.rev].cap -= d; res += d; if (res == up) return res; } level[v] = n; return res; }; Cap flow = 0; while (flow < flow_limit){ bfs(); if (level[t] == -1) break; std::fill(iter.begin(), iter.end(), 0); Cap f = dfs(dfs, t, flow_limit - flow); if (!f) break; flow += f; } return flow; } std::vector min_cut(int s){ std::vector visited(n); internal::simple_queue que; que.push(s); visited[s] = true; while (!que.empty()){ int v = que.front(); que.pop(); for (auto e : g[v]){ if (e.cap && !visited[e.to]){ visited[e.to] = true; que.push(e.to); } } } return visited; } private: int n; struct _edge { int to, rev; Cap cap; }; std::vector> pos; internal::csr<_edge> g; std::vector degree; }; } // namespace noya2 #line 4 "/Users/noya2/Desktop/Noya2_library/flow/project_selection.hpp" #line 6 "/Users/noya2/Desktop/Noya2_library/flow/project_selection.hpp" namespace noya2 { template struct project_selection { int n; // Costs c0 without any constraints. Cost c0; // It costs c1[ 2*i + x_i ]. std::vector c1; maxflow mfg; project_selection () {} project_selection (int _n) : n(_n), c0(0), c1(_n*2, 0), mfg(n+2) {} void cost_total(Cost cost){ c0 += cost; } void profit_total(Cost profit){ c0 -= profit; } void cost_x0(int x, Cost cost){ c1[2*x + 0] += cost; } void cost_x1(int x, Cost cost){ c1[2*x + 1] += cost; } void cost_xp(int x, std::array cost){ c1[2*x + 0] += cost[0]; c1[2*x + 1] += cost[1]; } void profit_x0(int x, Cost profit){ c1[2*x + 0] -= profit; } void profit_x1(int x, Cost profit){ c1[2*x + 1] -= profit; } void profit_xp(int x, std::array profit){ c1[2*x + 0] -= profit[0]; c1[2*x + 1] -= profit[1]; } void cost_x0y1(int x, int y, Cost c){ assert(c >= 0); mfg.add_edge(x, y, c); } void cost_x1y0(int x, int y, Cost c){ cost_x0y1(y, x, c); } void cost_xpyq(int x, int y, std::array,2> cost){ c0 += cost[0][0]; cost_x1(x, cost[1][0] - cost[0][0]); cost_x1(y, cost[1][1] - cost[1][0]); cost_x0y1(x, y, cost[1][0] + cost[0][1] - cost[0][0] - cost[1][1]); } void profit_x0y0(int x, int y, Cost profit){ profit_total(profit); cost_x1(x, profit); cost_x0y1(x, y, profit); } void profit_x1y1(int x, int y, Cost profit){ profit_x1(y, profit); cost_x0y1(x, y, profit); } Cost min_cost(){ int s = n, t = n+1; for (int x = 0; x < n; x++){ Cost x0 = c1[2*x + 0]; Cost x1 = c1[2*x + 1]; c0 += std::min(x0, x1); if (x0 < x1){ mfg.add_edge(s, x, x1 - x0); } if (x0 > x1){ mfg.add_edge(x, t, x0 - x1); } } Cost ans = c0 + mfg.flow(s, t); return ans; } Cost max_profit(){ return -min_cost(); } std::vector answer(){ auto ans = mfg.min_cut(n); ans.pop_back(); // pop s ans.pop_back(); // pop t for (auto &x : ans){ x = !x; } return ans; } }; } // namespace noya2 #line 4 "c.cpp" void solve(){ int n, m; in(n,m); vector a(n), b(m); in(a,b); project_selection ps(n+m); rep(i,m){ int k; in(k); while (k--){ int x; in(x); x--; ps.cost_x0y1(x, n+i, iinf); } ps.profit_x1(n+i, b[i]); } rep(i,n){ ps.cost_x1(i, a[i]); } out(ps.max_profit()); } int main(){ int t = 1; //in(t); while (t--) { solve(); } }