#include using namespace std; #define MAX 0xFFFFFFFFFFFFFFFF #define all(x) begin(x), end(x) int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); int64_t N; cin >> N; map> x_to_min_max; for (int64_t i = 0; i < N; ++i) { int64_t x, y; cin >> x >> y; auto it = x_to_min_max.find(x); if (it != x_to_min_max.end()) { auto& min_max = it->second; min_max.first = min(min_max.first, y); min_max.second = max(min_max.second, y); } else { x_to_min_max[x] = {y, y}; } } uint64_t len = 0; vector xs; for(auto v: x_to_min_max) { auto min_max = v.second; len += min_max.second - min_max.first; xs.push_back(v.first); } len += xs[xs.size()-1] - xs[0]; int64_t best_left = 0; int64_t best_right = 0; int64_t left = x_to_min_max[xs[0]].first; int64_t right = x_to_min_max[xs[0]].second; for (int64_t i = 0; i < xs.size(); ++i){ int64_t new_left = x_to_min_max[xs[i]].first; int64_t new_right = x_to_min_max[xs[i]].second; best_left = min( best_left + abs(left - new_right), best_right + abs(right - new_right) ); best_right = min( best_left + abs(left - new_left), best_right + abs(right - new_left) ); left = new_left; right = new_right; } len += min(best_left, best_right); cout << len << endl; return 0; }