#include #include #include #include using namespace std; int main() { map> volcanoes; vector>> sorted; int samples, x, y; cin >> samples; while(samples--){ cin >> x >> y; if (auto it = volcanoes.find(x) != volcanoes.end()){ volcanoes.at(it).first = max(y, volcanoes.at(it).first); volcanoes.at(it).second = min(y, volcanoes.at(it).second); }else{ volcanoes.insert({x, make_pair(y,y)}); } } //first max int distance = 0, last_x = -1, pos_y = volcanoes.begin()->second.first; for (auto it:volcanoes) { distance += abs(it.second.first - it.second.second); if (last_x > 0) { distance += it.first - last_x; if (abs(pos_y - it.second.second) < abs(pos_y - it.second.first)){ distance+=abs(pos_y - it.second.second); pos_y = it.second.first; } else { distance+=abs(pos_y - it.second.first); pos_y = it.second.second; } } last_x = it.first; } int result = distance; //first min distance = 0, last_x = -1, pos_y = volcanoes.begin()->second.second; for (auto it:volcanoes) { distance += abs(it.second.first - it.second.second); if (last_x > 0) { distance += it.first - last_x; if (abs(pos_y - it.second.second) < abs(pos_y - it.second.first)){ distance+=abs(pos_y - it.second.second); pos_y = it.second.first; } else { distance+=abs(pos_y - it.second.first); pos_y = it.second.second; } } last_x = it.first; } result = min(result, distance); cout << result; return 0; }