#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
//string Alice(int x, int k) {
// string bin(30, '0');
// for (int i = 0; i < 30; i++)
// bin[i] = (x >> i) & 1 ? 'I' : 'O';
// string odd;
// for (int i = 1; i < 30; i += 2)
// odd += bin[i];
// return bin + 'O' + odd;
//}
//
//int str2num(string s) {
// int ans = 0;
// for (int i = 0; i < 30; i++)
// if (s[i] == 'I')
// ans |= 1 << i;
// return ans;
//}
//
//int Bob(string s, int k) {
// string bin = s.substr(0, 30);
// if (s[30] == 'I')
// return str2num(bin);
//
// for (int i = 1, j = 31; j < s.size(); i += 2, j++) {
// cout << s[i] << " " << s[j] << '\n';
// if (s[i] == s[j]) continue;
// if (s[j] == 'I') {
// return str2num(bin);
// } else {
// swap(bin[i], bin[i+1]);
// return str2num(bin);
// }
// }
// return str2num(bin);
//}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr); std::cout.tie(nullptr);
int n; cin >> n;
long long P[n+1];
P[0] = 0;
for (int i = 0, b; i < n; i++) {
cin >> P[i+1] >> b;
P[i+1] += P[i] - b;
}
long long ans = 0;
for (int i = 0; i <= n; i++) {
if (P[i] > P[n]) {
ans += P[i] - P[n];
P[i] = P[n];
}
if (P[i] < 0) {
ans += -P[i];
P[i] = 0;
}
}
priority_queue<long long> q;
for (int i = 1; i < n; i++) {
q.push(P[i]);
q.push(P[i]);
ans += q.top() - P[i];
q.pop();
}
cout << ans;
}