#include using namespace std; using lint = long long; template using V = vector; template using VV = V< V >; struct SegmentTree { using T = int; static T op(const T& a, const T& b) { return min(a, b); } static constexpr T e() { return 2e9; } const int n; V t; SegmentTree(int n) : n(n), t(2 * n, e()) {} T& operator[](int i) { return t[i + n]; } void build() { for (int i = n - 1; i; --i) t[i] = op(t[2 * i], t[2 * i + 1]); } T acc(int l, int r) const { T resl = e(), resr = e(); for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) resl = op(resl, t[l++]); if (r & 1) resr = op(t[--r], resr); } return op(resl, resr); } void set(int i, const T& a) { t[i += n] = a; while (i >>= 1) t[i] = op(t[2 * i], t[2 * i + 1]); } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; V c(n + 1), d(n + 1); for (int i = 0; i < n; ++i) cin >> c[i] >> d[i]; SegmentTree st(n); for (int i = n - 1; i >= 0; --i) { st[i] = c[i]; c[i] += c[i + 1]; d[i] += d[i + 1]; } st.build(); V dp(n + 1, 1e18); dp[n] = 0; for (int i = n - 1; i >= 0; --i) { for (int j = i + 1; j <= n; ++j) { lint curr = (c[i] - c[j]) + (d[i + 1] - d[j]) + st.acc(i, j); dp[i] = min(dp[i], curr + dp[j]); } } cout << dp[0] << '\n'; }