对map容器的理解

B. Businessmen Problems
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nn distinct chemical elements with indices a1,a2,,ana1,a2,…,an, and will get an income of xixi Berland rubles if the ii-th element from this list is in the set of this company.

The TopChemist company discovered mm distinct chemical elements with indices b1,b2,,bmb1,b2,…,bm, and it will get an income of yjyj Berland rubles for including the jj-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 nn (1n1051≤n≤105)  — the number of elements discovered by ChemForces.

The ii-th of the next nn lines contains two integers aiai and xixi (1ai1091≤ai≤1091xi1091≤xi≤109)  — the index of the ii-th element and the income of its usage on the exhibition. It is guaranteed that all aiai are distinct.

The next line contains a single integer mm (1m1051≤m≤105)  — the number of chemicals invented by TopChemist.

The jj-th of the next mm lines contains two integers bjbj and yjyj, (1bj1091≤bj≤1091yj1091≤yj≤109)  — the index of the jj-th element and the income of its usage on the exhibition. It is guaranteed that all bjbj 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,73,7), while TopChemist can choose (1,2,41,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 109109, while TopChemist can choose (14,92,3514,92,35). This way the total income is (239)+(15+65+89)=408(239)+(15+65+89)=408


题意:输入两组数据,每组数据第一个是序号,第二个是收入,相同序号.取最大,输出全部收入

#include <cstdio>
#include <map>
using namespace std;
map<int,int>sum;//考对map容器的使用
int a[2];
int main()
{
    long long ans=0;
    int x,y;
    for(int i=0;i<2;i++)
    {
        scanf("%d",&a[i]);
        for(int j=0;j<a[i];j++)
        {
            scanf("%d%d",&x,&y);
            ans-=sum[x];//减去相同的如1 4 与1 4
            ans+=max(y,sum[x]);
            sum[x]=y;
        }
    }
    printf("%lld\n",ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值