#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, cnt, tot, sum, a[200005], b[200005], c[200005], d[200005];
bool f[200005];
signed main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i] >> b[i];
c[++cnt] = a[i];
c[++cnt] = b[i];
}
sort(c + 1, c + cnt + 1);
for(int i = 1; i <= cnt; i++){
if(c[i] != c[i + 1] || i == 1){
d[++tot] = a[i];
}
}
for(int i = 1; i <= n; i++){
int x = lower_bound(d + 1, d + tot + 1, a[i]) - d;
int y = lower_bound(d + 1, d + tot + 1, b[i]) - d;
for(int j = x; j < y; j++){
f[j] = 1;
}
}
for(int i = 1; i < tot; i++){
if(f[i] == 1){
sum += d[i + 1] + d[i];
}
}
cout << sum;
return 0;
}
求助!!!