#define LOCAL #include using namespace std; #pragma region Macros typedef long long ll; typedef __int128_t i128; typedef unsigned int uint; typedef unsigned long long ull; #define ALL(x) (x).begin(), (x).end() template istream& operator>>(istream& is, vector& v) { for (T& x : v) is >> x; return is; } template ostream& operator<<(ostream& os, const vector& v) { for (size_t i = 0; i < v.size(); i++) { os << v[i] << (i + 1 == v.size() ? "" : " "); } return os; } template ostream& operator<<(ostream& os, const pair& p) { os << '(' << p.first << ',' << p.second << ')'; return os; } template ostream& operator<<(ostream& os, const map& m) { os << '{'; for (auto itr = m.begin(); itr != m.end();) { os << '(' << itr->first << ',' << itr->second << ')'; if (++itr != m.end()) os << ','; } os << '}'; return os; } template ostream& operator<<(ostream& os, const unordered_map& m) { os << '{'; for (auto itr = m.begin(); itr != m.end();) { os << '(' << itr->first << ',' << itr->second << ')'; if (++itr != m.end()) os << ','; } os << '}'; return os; } template ostream& operator<<(ostream& os, const set& s) { os << '{'; for (auto itr = s.begin(); itr != s.end();) { os << *itr; if (++itr != s.end()) os << ','; } os << '}'; return os; } template ostream& operator<<(ostream& os, const multiset& s) { os << '{'; for (auto itr = s.begin(); itr != s.end();) { os << *itr; if (++itr != s.end()) os << ','; } os << '}'; return os; } template ostream& operator<<(ostream& os, const unordered_set& s) { os << '{'; for (auto itr = s.begin(); itr != s.end();) { os << *itr; if (++itr != s.end()) os << ','; } os << '}'; return os; } template ostream& operator<<(ostream& os, const deque& v) { for (size_t i = 0; i < v.size(); i++) { os << v[i] << (i + 1 == v.size() ? "" : " "); } return os; } template ostream& operator<<(ostream& os, const array& v) { for (size_t i = 0; i < N; i++) { os << v[i] << (i + 1 == N ? "" : " "); } return os; } template void print_tuple(ostream&, const T&) {} template void print_tuple(ostream& os, const T& t) { if (i) os << ','; os << get(t); print_tuple(os, t); } template ostream& operator<<(ostream& os, const tuple& t) { os << '{'; print_tuple<0, tuple, Args...>(os, t); return os << '}'; } void debug_out() { cerr << '\n'; } template void debug_out(Head&& head, Tail&&... tail) { cerr << head; if (sizeof...(Tail) > 0) cerr << ", "; debug_out(move(tail)...); } #ifdef LOCAL #define debug(...) \ cerr << " "; \ cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n'; \ cerr << " "; \ debug_out(__VA_ARGS__) #else #define debug(...) void(0) #endif template T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; } template T lcm(T x, T y) { return x / gcd(x, y) * y; } int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); } int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); } int popcount(signed t) { return __builtin_popcount(t); } int popcount(long long t) { return __builtin_popcountll(t); } bool ispow2(int i) { return i && (i & -i) == i; } long long MSK(int n) { return (1LL << n) - 1; } template T ceil(T x, T y) { assert(y >= 1); return (x > 0 ? (x + y - 1) / y : x / y); } template T floor(T x, T y) { assert(y >= 1); return (x > 0 ? x / y : (x - y + 1) / y); } template inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return true; } return false; } template void mkuni(vector& v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } template int lwb(const vector& v, const T& x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } #pragma endregion #include #include #include #include #include #include "atcoder/internal_queue" namespace ProjectSelectionProblem_Impl { 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; } int add_vertex() { g.resize(_n + 1); return _n++; } 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); atcoder::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); atcoder::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; }; template struct ProjectSelectionProblem { int n, s, t; T sum; const T inf = std::numeric_limits::max() / 2; mf_graph graph; ProjectSelectionProblem(int n) : n(n + 2), s(n), t(n + 1), sum(T(0)), graph(n + 2) {} void x_false_loss(int x, T z) { assert(0 <= x and x < n); graph.add_edge(x, t, z); } void x_false_profit(int x, T z) { assert(0 <= x and x < n); sum += z; x_true_loss(x, z); } void x_true_loss(int x, T z) { assert(0 <= x and x < n); graph.add_edge(s, x, z); } void x_true_profit(int x, T z) { assert(0 <= x and x < n); sum += z; x_false_loss(x, z); } void x_false_y_true_loss(int x, int y, T z) { assert(0 <= x and x < n); assert(0 <= y and y < n); graph.add_edge(x, y, z); } void x_true_y_false_loss(int x, int y, T z) { assert(0 <= x and x < n); assert(0 <= y and y < n); graph.add_edge(y, x, z); } void x_false_y_false_profit(int x, int y, T z) { assert(0 <= x and x < n); assert(0 <= y and y < n); sum += z; int w = graph.add_vertex(); n++; x_true_loss(w, z); x_false_y_true_loss(w, x, inf); x_false_y_true_loss(w, y, inf); } void x_true_y_true_profit(int x, int y, T z) { assert(0 <= x and x < n); assert(0 <= y and y < n); sum += z; int w = graph.add_vertex(); n++; x_false_loss(w, z); x_true_y_false_loss(w, x, inf); x_true_y_false_loss(w, y, inf); } T min_loss() { return graph.flow(s, t) - sum; } T max_profit() { return -min_loss(); } }; } // namespace ProjectSelectionProblem_Impl using ProjectSelectionProblem_Impl::ProjectSelectionProblem; const int INF = 1e9; const long long IINF = 1e14; const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; const char dir[4] = {'D', 'R', 'U', 'L'}; const long long MOD = 1000000007; // const long long MOD = 998244353; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M, K, P; cin >> N >> M >> K >> P; ProjectSelectionProblem PSP(N + M + K); for (int i = 0; i < N; i++) { int E; cin >> E; PSP.x_true_profit(i, E); } for (int i = 0; i < M; i++) { int F; cin >> F; PSP.x_false_profit(N + i, F); } for (int i = 0; i < K; i++) { int V; cin >> V; PSP.x_true_loss(N + M + i, V); } for (int i = 0; i < N; i++) { int L; cin >> L; for (; L--;) { int A; cin >> A; A--; PSP.x_true_y_false_loss(i, N + M + A, IINF); } } for (int i = 0; i < P; i++) { int I, J; cin >> I >> J; I--, J--; PSP.x_true_y_false_loss(I, N + J, IINF); } ll ans = PSP.max_profit(); cout << ans << '\n'; vector used(N + M + K, false); auto list = PSP.graph.min_cut(PSP.s); for (int i = 0; i < N + M + K; i++) { if (i < N and !list[i]) used[i] = true; if (N <= i and i < N + M and list[i]) used[i] = true; if (N + M <= i and !list[i]) used[i] = true; } int sum = accumulate(used.begin(), used.end(), 0); cout << sum << '\n'; for (int i = N + M; i < N + M + K; i++) { if (used[i]) { cout << "Preparation " << i - (N + M) + 1 << '\n'; } } for (int i = 0; i < N; i++) { if (used[i]) { cout << "Goal " << i + 1 << '\n'; } } for (int i = N; i < N + M; i++) { if (used[i]) { cout << "Action " << i - N + 1 << '\n'; } } return 0; }