#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5, VAL = 3 * N, MAX = 4 * VAL;
int n, k, a[N], b[N], t[N], pos[N];
int ans[N];
int seg[MAX];
vector<int> val;
void upd(int id, int tl, int tr, int i, int x){
if(i < tl || i > tr) return;
if(tl == tr){
seg[id] = max(seg[id], x); return;
}
int mid = (tl + tr) / 2;
upd(id * 2, tl, mid, i, x), upd(id * 2 + 1, mid + 1, tr, i, x);
seg[id] = max(seg[id * 2], seg[id * 2 + 1]);
}
int query(int id, int tl, int tr, int l, int r){
if(r < tl || tr < l) return 0;
if(l <= tl && tr <= r) return seg[id];
int mid = (tl + tr) / 2;
return max(query(id * 2, tl, mid, l, r), query(id * 2 + 1, mid + 1, tr, l, r));
}
int bit[MAX];
void upd(int x, int val){
for(; x < MAX; x += x & -x){
bit[x] += val;
}
}
void upd(int l, int r, int val){
l++, r++;
upd(l, val), upd(r + 1, -val);
}
int query(int x){
x++;
int ans = 0;
for(; x; x -= x & -x) ans += bit[x];
return ans;
}
vector<int> p[N];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i] >> b[i], val.insert(val.end(), {a[i], b[i]});
for(int i = 1; i <= k; i++) cin >> t[i], val.push_back(t[i]);
sort(val.begin(), val.end()); val.resize(unique(val.begin(), val.end()) - val.begin());
for(int i = 1; i <= n; i++) a[i] = lower_bound(val.begin(), val.end(), a[i]) - val.begin(), b[i] = lower_bound(val.begin(), val.end(), b[i]) - val.begin();
for(int i = 1; i <= k; i++) t[i] = lower_bound(val.begin(), val.end(), t[i]) - val.begin();
for(int i = 1; i <= k; i++) upd(1, 0, val.size() - 1, t[i], i);
for(int i = 1; i <= n; i++){
pos[i] = query(1, 0, val.size() - 1, min(a[i], b[i]), max(a[i], b[i]));
p[pos[i]].push_back(i);
}
for(int i = k; i >= 0; i--){
if(i) upd(0, t[i], 1);
for(int x : p[i]){
int cnt = query(max(a[x], b[x]));
if(i == 0){
ans[x] = (cnt & 1 ? b[x] : a[x]);
} else{
if(a[x] > b[x]) swap(a[x], b[x]);
ans[x] = (cnt & 1 ? a[x] : b[x]);
}
}
}
long long res = 0;
for(int i = 1; i <= n; i++) res += val[ans[i]];
cout << res << '\n';
return 0;
}
Compilation message
cc1plus: error: '::main' must return 'int'