#include <bits/stdc++.h> using namespace std; const int maxn = 20005; int n; int sum = 0; int a[maxn], b[maxn]; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; } sort(a, a + n); sort(b, b + n); for (int i = 0; i < n; i++) { sum += b[i] - a[i]; if (i + 1 < n) { // 不是最后一条线段 if (b[i] > a[i + 1]) { // 与后一条线段有重复 sum -= (b[i] - a[i + 1]); } } } cout << sum; }