Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int const N = 1e5 + 1;
int n, m;
map<int, int> a, b;
vector<int> all;
int main() {
scanf("%d", &n);
for(int i = 0, x, y; i < n; ++i) {
scanf("%d %d", &x, &y);
a[x] = y;
all.push_back(x);
}
scanf("%d", &m);
for(int i = 0, x, y; i < m; ++i) {
scanf("%d %d", &x, &y);
b[x] = y;
all.push_back(x);
}
sort(all.begin(), all.end());
all.resize(unique(all.begin(), all.end()) - all.begin());
long long res = 0;
for(int i = 0, mx; i < all.size(); ++i) {
mx = 0;
if(a.count(all[i]))
mx = max(mx, a[all[i]]);
if(b.count(all[i]))
mx = max(mx, b[all[i]]);
res += mx;
}
printf("%lld\n", res);
return 0;
}
Copy The Code &
Try With Live Editor
Input
3
1 2
7 2
3 10
4
1 4
2 4
3 4
4 4
1 2
7 2
3 10
4
1 4
2 4
3 4
4 4
Output
24
Demonstration
Codeforces Solution-B. Businessmen Problems-Solution in C, C++, Java, Python