#include #define rep(i, n) for (int i = 0; i < (n); i++) #define repr(i, n) for (int i = (n) - 1; i >= 0; i--) using namespace std; using ll = long long; void chmin(ll &x, ll y) { x = min(x, y); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N; cin >> N; vector C(N + 1), D(N + 1); rep(i, N) cin >> C[i] >> D[i]; const ll INF = 1e18; vector dp(N + 1, INF); vector ep(N + 1, INF); dp[0] = -D[0]; rep(i, N) { chmin(dp[i + 1], dp[i] - D[i + 1] + C[i]); chmin(dp[i + 1], dp[i]); chmin(ep[i + 1], dp[i] + C[i]); chmin(ep[i + 1], ep[i]); chmin(dp[i + 1], ep[i] - D[i + 1]); } ll sumc = 0, sumd = 0; rep(i, N) sumc += C[i], sumd += D[i]; cout << ep[N] + sumc + sumd << endl; }