#include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; int main(){ int n; cin >> n; vector a(2 * n); for (int i = 0; i < 2 * n; i++){ cin >> a[i]; } int left = 0, right = 2 * n - 1; ll ans = 0; for (int i=0; i a[right]-a[right-1]){ if(left > n-2){ break; } ans += a[left] - a[left + 1]; //cout << "l"; left += 2; }else{ if(right<1){ break; } ans += a[right] - a[right - 1]; //cout << "r" << a[right] << " "; right -= 2; } } cout << ans << "\n"; return 0; }