#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define sz size() #define pb push_back #define mp make_pair #define fi first #define se second #define all(c) (c).begin(), (c).end() #define rep(i,a,b) for(int i=(a);i<(b);++i) #define clr(a, b) memset((a), (b) ,sizeof(a)) #define MOD 1000000009 #define NN 82 #define INF 100000000 int v[NN][NN]; int flag[NN]; int flow(int s, int e, int f){ if(s == e)return f; flag[s] = 1; for(int i = 0; i < NN; i++){ if(v[s][i] > 0 && flag[i] == 0){ int d = flow(i, e, min(f, v[s][i])); if(d > 0){ v[s][i] -= d; v[i][s] += d; return d; } } } return 0; } void ae(int from, int to, int cost){ v[from][to] = cost; v[to][from] = 0; } int main(){ clr(v,-1); int sum = 0; int n; cin >> n; rep(i,0,n){ int b,c; cin >> b >> c; ae(2*n,i,b); ae(i+n,2*n+1,c); ae(i,i+n,INF); sum += b+c; } int m; cin >> m; rep(i,0,m){ int d,e; cin >> d >> e; ae(d,e+n,INF); } int ans = 0; for(;;){ memset(flag,0,sizeof(flag)); int f = flow(2*n,2*n+1, INF); if(f == 0)break; ans += f; } cout << sum-ans << endl; return 0; } /* 追加した辺の逆辺に 0 を入れていなかったところを修正。 */