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 (1≤n≤1051≤n≤105)  — the number of elements discovered by ChemForces.

The ii-th of the next nn lines contains two integers aiai and xixi (1≤ai≤1091≤ai≤109, 1≤xi≤1091≤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 (1≤m≤1051≤m≤105)  — the number of chemicals invented by TopChemist.

The jj-th of the next mm lines contains two integers bjbj and yjyj, (1≤bj≤1091≤bj≤109, 1≤yj≤1091≤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.

题意: 给你n个A公司的新元素及价值,再给你m个B公司的新元素及价值,两个公司的元素相同的取较大值,问两家公司元素价值总和

直接用一个map保存下相同元素的较大值,然后遍历相加就可以了

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e5 + ;
const int mod = 1e9 + ;
typedef long long ll;
int main(){
std::ios::sync_with_stdio(false);
ll n, m;
while( cin >> n ) {
map<ll,ll> mm;
ll x, y;
for( ll i = ; i < n; i ++ ) {
cin >> x >> y;
if( mm[x] < y ) {
mm[x] = y;
}
}
cin >> m;
for( ll i = ; i < m; i ++ ) {
cin >> x >> y;
if( mm[x] < y ) {
mm[x] = y;
}
}
ll sum = ;
for( map<ll,ll>:: iterator it = mm.begin(); it != mm.end(); it ++ ) {
sum += (*it).second;
}
cout << sum << endl;
}
return ;
}

最新文章

  1. Express URL跳转(重定向)的实现
  2. js通过注册表找到本地软件安装路径并且执行
  3. .NET生成静态页面并分页
  4. Python-7 列表list
  5. win32 公用对话框
  6. YTU 3022: 完全二叉树(1)
  7. Appnium安装——Mac篇
  8. javascript之六种数据类型以及特殊注意点
  9. error “base class has incomplete type”
  10. 点击穿透问题(http://www.tuicool.com/articles/6NfaUnM)
  11. mongodb的副本集总结
  12. 跨域资源共享 CORS 详解
  13. JavaScript 覆盖document.createElement 方法 解决window.close在火狐下不兼容问题)
  14. Java中读取某个目录下的所有文件和文件夹
  15. GridView规则显示图片
  16. BIZ中model.getSql源码分析
  17. git冲突管理
  18. c语言操作符总结
  19. Java 8 学习资料汇总【转载】
  20. 【转】bootstrap实现左侧菜单伸缩

热门文章

  1. bit、byte、kb、mb、g的区别
  2. Windows 纠错
  3. Axis1.4 配置数组类型复杂对象
  4. Unity学习--捕鱼达人笔记
  5. java并发编程(四)----(JUC)Lock锁初探
  6. 强化学习(Reinforcement Learning)中的Q-Learning、DQN,面试看这篇就够了!
  7. java中安全的单例与不安全的单例
  8. Joda Time使用小结
  9. jjyq app登录接口报:“内容类型不支持”???已解决
  10. JavaScript的垃圾回收机制与内存泄漏