#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)}); } } int distance = 0, last_x = -1, last_min = -1, last_max = -1, counter = 0; for (auto it:volcanoes) { distance += abs(it.second.first - it.second.second); if (last_x > 0) { distance += it.first - last_x; int from_max = abs(last_max - it.second.second) < abs(last_max - it.second.first) ? abs(last_max - it.second.second) : abs(last_max - it.second.first); int from_min = abs(last_min - it.second.second) < abs(last_min - it.second.first) ? abs(last_min - it.second.second) : abs(last_min - it.second.first); if (from_max < from_min) { distance += from_max; } else { distance += from_min; } } last_x = it.first; last_max = it.second.first; last_min = it.second.second; } cout << distance; return 0; }