http://www.elijahqi.win/archives/3535
Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies.
In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies.
All elements are enumerated with integers. The ChemForces company has discovered n
n
distinct chemical elements with indices a1,a2,…,an
a1,a2,…,an
, and will get an income of xi
xi
Berland rubles if the i
i
-th element from this list is in the set of this company.
The TopChemist company discovered m
m
distinct chemical elements with indices b1,b2,…,bm
b1,b2,…,bm
, and it will get an income of yj
yj
Berland rubles for including the j
j
-th element from this list to its set.
In other words, the first company can present any subset of elements from {a1,a2,…,an}
{a1,a2,…,an}
(possibly empty subset), the second company can present any subset of elements from {b1,b2,…,bm}
{b1,b2,…,bm}
(possibly empty subset). There shouldn’t be equal elements in the subsets.
Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible.
Input
The first line contains a single integer n
n
(1≤n≤105
1≤n≤105
) — the number of elements discovered by ChemForces.
The i
i
-th of the next n
n
lines contains two integers ai
ai
and xi
xi
(1≤ai≤109
1≤ai≤109
, 1≤xi≤109
1≤xi≤109
) — the index of the i
i
-th element and the income of its usage on the exhibition. It is guaranteed that all ai
ai
are distinct.
The next line contains a single integer m
m
(1≤m≤105
1≤m≤105
) — the number of chemicals invented by TopChemist.
The j
j
-th of the next m
m
lines contains two integers bj
bj
and yj
yj
, (1≤bj≤109
1≤bj≤109
, 1≤yj≤109
1≤yj≤109
) — the index of the j
j
-th element and the income of its usage on the exhibition. It is guaranteed that all bj
bj
are distinct.
Output
Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets.
Examples
input
Copy
3
1 2
7 2
3 10
4
1 4
2 4
3 4
4 4
output
Copy
24
input
Copy
1
1000000000 239
3
14 15
92 65
35 89
output
Copy
408
Note
In the first example ChemForces can choose the set (3,7
3,7
), while TopChemist can choose (1,2,4
1,2,4
). This way the total income is (10+2)+(4+4+4)=24
(10+2)+(4+4+4)=24
.
In the second example ChemForces can choose the only element 109
109
, while TopChemist can choose (14,92,35
14,92,35
). This way the total income is (239)+(15+65+89)=408
(239)+(15+65+89)=408
.
stl大法好 直接使用map 最后遍历即可
#include<map>
#include<cstdio>
#include<cctype>
#include<algorithm>
#define ll long long
using namespace std;
map<int,int> mm;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if(T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
return x*f;
}
int n,m;
int main(){
// freopen("b.in","r",stdin);
n=read();int x;
for (int i=1;i<=n;++i) x=read(),mm[x]=max(mm[x],read());
m=read();
for (int i=1;i<=m;++i) x=read(),mm[x]=max(mm[x],read());
ll ans=0;
for (auto it=mm.begin();it!=mm.end();++it) ans+=it->second;
printf("%I64d\n",ans);
return 0;
}