#include "bits/stdc++.h" #define in std::cin #define out std::cout #define rep(i,N) for(LL i=0;inode; public: void init(LL size) { n = 1; while (n < size) n *= 2; node.resize(2 * n - 1, inf); } // node[i] += x void update(LL i, LL x) { i += n - 1; node[i] = x; while (i > 0) { i = (i - 1) / 2; node[i] = std::min(node[2 * i + 1], node[2 * i + 2]); } } // min([a, b)) // ex.) [0, N - 1] == [0, N) LL get_min(LL a, LL b, LL k = 0, LL l = 0, LL r = -1) { if (r < 0) r = n; if (r <= a || b <= l) return inf; if (a <= l && r <= b) return node[k]; LL vl = get_min(a, b, 2 * k + 1, l, (l + r) / 2); LL vr = get_min(a, b, 2 * k + 2, (l + r) / 2, r); return std::min(vl, vr); } }; LL N; std::vectorC, D, memo; SegmentTree tree; LL dp(LL i) { if (i == N) return 0; if (memo[i] != -1) return memo[i]; LL res = inf; for (LL j = i; j < N; ++j) { res = std::min(res, dp(j + 1) - D[i] + tree.get_min(i, j + 1)); } return memo[i] = res; } int main() { in >> N; C.resize(N); D.resize(N); rep(i, N) in >> C[i] >> D[i]; LL ans = 0; rep(i, N) ans += C[i] + D[i]; tree.init(N); rep(i, N) tree.update(i, C[i]); memo.resize(N, -1); ans += dp(0); out << ans << std::endl; }