#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #include using std::cout; using namespace std; using ll=long long; using ld=long double; using P=array; using mint = atcoder::modint998244353; #define rep(i,n) for (ll i=0,siz=(n);i=siz;i--) #define popcount __builtin_popcountll #define cin(a) ll a; cin >> a; #define cin2(a,b) ll a,b; cin >> a >> b; #define cin3(a,b,c) ll a,b,c; cin >> a >> b >> c; #define cinvec(v) vector v(N); rep(i,N) cin >> v[i]; #define cinvec2(v,n) vector v(n); rep(i,n) cin >> v[i]; #define cins(s) string s; cin >> s; #define cinc(c) char c; cin >> c; vector dx = {0,-1,0,1},dy = {1,0,-1,0}, ddx = {0,-1,-1,-1,0,1,1,1}, ddy = {1,1,0,-1,-1,-1,0,1}; void yesno(bool b) {cout << (b?"Yes":"No") << endl;} template void sortunique(vector &V) {sort(V.begin(), V.end()); V.erase(unique(V.begin(), V.end()), V.end());} template void outvec(const std::initializer_list& list) { bool first = true; for (const auto& elem : list) { if (!first) std::cout << " "; std::cout << elem; first = false; } std::cout << std::endl; } template void outvec(const std::vector& vec) { bool first = true; for (const auto& elem : vec) { if (!first) std::cout << " "; std::cout << elem; first = false; } std::cout << std::endl; } template void outvecp(const std::vector>& vec) { bool first = true; for (const auto& elem : vec) { if (!first) std::cout << " "; for (int i = 0; i < N; i++) std::cout << elem[i] << " "; first = false; } std::cout << std::endl; } vector compress(vector &A){vector B = A; sortunique(B); rep(i,A.size()) A[i] = lower_bound(B.begin(),B.end(),A[i]) - B.begin(); return B;} ll bs(ll l, ll r, function f) { while (r-l > 1) { ll m = (l+r)/2; if (f(m)) r = m; else l = m; } return r; } ll msb(ll N) { assert(N>0); return 63 - __builtin_clzll(N); } class Dinic_MaximFlow{ private: struct Edge{ ll to; ll cap; ll rev; }; vector> G; vector level; vector iter; ll N; void bfs(ll s){ for (ll i=0;i que; level[s] = 0; que.push(s); while(!que.empty()){ ll v = que.front(); que.pop(); for (ll i=0;i 0 && level[e.to] < 0){ level[e.to] = level[v] + 1; que.push(e.to); } } } } ll dfs(ll v, ll t, ll f){ if (v == t) return f; for (ll &i=iter[v];i 0 && level[v] < level[e.to]){ ll d = dfs(e.to, t, min(f, e.cap)); if (d > 0){ e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } public: Dinic_MaximFlow(ll n){ N = n; G.resize(n); level.resize(n); iter.resize(n); } void add_edge(ll from, ll to, ll cap){ G[from].push_back((Edge){to, cap, (ll)G[to].size()}); G[to].push_back((Edge){from, 0, (ll)G[from].size()-1}); } ll max_flow(ll s, ll t){ ll flow = 0; while (true){ bfs(s); if (level[t] < 0) return flow; for (ll i=0;i 0){ flow += f; } } } vector> edges(){ return G; } vector

matching_res(ll Ns){ vector

ans; for (ll i=0;i B(N),C(N); rep(i,N) cin >> B[i] >> C[i]; cin(M); vector D(M),E(M); rep(i,M) cin >> D[i] >> E[i]; Dinic_MaximFlow dm(2*N+2); ll st = 2*N,goal = 2*N+1; rep(i,N) { dm.add_edge(st,i,B[i]+C[i]); dm.add_edge(i,i+N,1e9); dm.add_edge(i,goal,C[i]); dm.add_edge(st,i+N,B[i]); dm.add_edge(i+N,goal,B[i]+C[i]); } rep(i,M) { dm.add_edge(D[i],E[i]+N,1e9); } ll ans = 2*accumulate(C.begin(),C.end(),0LL) + 2*accumulate(B.begin(),B.end(),0LL) - dm.max_flow(st,goal); cout << ans << endl; return 0; }