//https://oj.uz/problem/view/LMIO19_bulves
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<iomanip>
#include <cassert>
#include<algorithm>
#include<queue>
#include <array>
#include <bitset>
#include<cstring>
using namespace std;
#define int long long
#define pii pair<int,int>
int INF = 1e18;
int a[500001];
int n;
const int lol = 30000;
void ez(){
int cur = 0;
int ans = 0;
for(int i = 1; i <= n; ++i){
cur += a[i];
ans += abs(cur);
}
cout << ans << "\n";
}
int sumA = 0;
int dp[30001]; int dp2[30001];
void onedp(){
vector<int>psums(n + 1, 0);
for(int i = 1; i <= n; ++i){
psums[i] = psums[i - 1] + a[i];
}
vector<vector<int>>dp(n + 2, vector<int>(2, INF)); //min cost to reach this state
dp[1][1] = 0;
for(int i = 1; i <= n; ++i){
for(int j = 0; j <= 1; ++j){
//if the psums is negative, you must transfer it all out (def not optimal to just drag it here)
if(j == 1){
//can choose to either transfer all or transfer not all
dp[i + 1][1] = min(dp[i + 1][j], dp[i][j] + abs(psums[i]));
if(psums[i] > 0){
dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + abs(psums[i]) - 1);
}
} else{
dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + abs(psums[i] - 1));
}
}
}
cout << min(dp[n + 1][0], dp[n + 1][1]) << "\n";
}
void seconddp(){
vector<int>psums(n + 1, 0);
for(int i = 1; i <= n; ++i){
psums[i] = psums[i - 1] + a[i];
}
//vector<vector<int>>dp(n + 2, vector<int>(2, INF)); //min cost to reach this state
//state: from 1...i-1 is all 0, if j==1 then it has the total prefix sums here, otherwise, it has one less than the total prefix sums, (didn't transfer one)
memset(dp, 0x3f, sizeof(dp));
memset(dp2, 0x3f, sizeof(dp2));
priority_queue<pii>pq;
dp[0] = 0;
int b = 0;
for(int i = 1; i <= n; ++i){
if(psums[i] > 0){
pq.push({0, -1});
pq.push({(psums[i]), 1});
pq.push({(psums[i]), 1});
pq.pop();
b += (psums[i]);
} else{
pq.push({0, 1});
b += abs(psums[i]);
pq.pop();
}
}
vector<pii>v;
while(!pq.empty()){
//cout << pq.top().first << " " << pq.top().second << "was from the pq" << endl;
if(!v.empty() && v.back().first == pq.top().first){
int x = v.back().first;
int dm = v.back().second + pq.top().second;
pq.pop();
v.pop_back();
v.push_back({x, dm});
} else{
v.push_back(pq.top());
pq.pop();
}
}
sort(v.begin(), v.end());
int m = 0;
int curx = 0;
int ans = 0;
for(int i = 0; i < v.size(); ++i){
//cout << curx << endl;
// cout << "lol\n";
if(v[i].first >= sumA){
v[i].first = sumA;
}
b += m * (v[i].first - curx);
curx = v[i].first;
if(v[i].first == sumA)break;
m += v[i].second;
}
cout << b << "\n";
}
int32_t main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i = 1; i <= n; ++i){
int x, y; cin >> x >> y;
a[i] = x - y;
sumA += x - y;
}
if(sumA == 0){
ez(); return 0;
}
seconddp(); return 0;
if(sumA == 1){
onedp();
} else{
seconddp();
}
}