#include using namespace std; #define int long long struct UnionFind{ vector tree; UnionFind(int n): tree(n+1, -1) {}; int root(int i){ return tree[i] < 0 ? i : tree[i] = root(tree[i]); } bool unite(int i, int j){ i = root(i), j = root(j); if(i == j) return false; tree[i] += tree[j], tree[j] = i; return true; } bool same(int i, int j){ return root(i) == root(j); } int size(int i){ return -tree[root(i)]; } }; signed main(){ int n; cin >> n; int ans = 0; int c, d; typedef pair> P; priority_queue, greater

> que; for(int i = 0;i < n;i++){ cin >> c >> d; ans += c; que.push({c, {0, i+1}}); if(i != 0) que.push({d, {i, i+1}}); } UnionFind uf(n); while(!que.empty()){ if(uf.unite(que.top().second.first, que.top().second.second)){ ans += que.top().first; } que.pop(); } cout << ans << endl; return 0; }