/* * Volcanoes.java * * Copyright 2022 ACM CTU Contest Team 53 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. * * */ import java.util.*; public class Volcanoes { public static class Volcano { int x; int y; public Volcano(int x, int y) { this.x = x; this.y = y; } } public static void sortByX(List volcs) { for (int i = 0; i < volcs.size(); i++) { for(int j = i + 1; j < volcs.size(); j++) { if (volcs.get(j).x < volcs.get(i).x) { Collections.swap(volcs, i, j); } } } } public static void sortByY(List volcs) { for (int i = 0; i < volcs.size(); i++) { for(int j = i + 1; j < volcs.size(); j++) { if (volcs.get(j).y < volcs.get(i).y) { Collections.swap(volcs, i, j); } } } } public static void sortVolcs(List volcs) { sortByY(volcs); sortByX(volcs); } public static void printVolcs(List volcs) { System.out.println("Volcanoes:"); for (int i = 0; i < volcs.size(); i++) { System.out.println(volcs.get(i).x + " " + volcs.get(i).y); } } public static int pathBetween(Volcano one, Volcano two) { int path = 0; path += Math.max(one.x, two.x) - Math.min(one.x, two.x); path += Math.max(one.y, two.y) - Math.min(one.y, two.y); return path; } public static int calcPath(List volcs) { int sum = 0; for(int i = 0; i < volcs.size() - 1; i++) { sum += pathBetween(volcs.get(i), volcs.get(i + 1)); } return sum; } public static void main (String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); List volcanoes = new ArrayList<>(); for(int i =0; i < n; i++) { Volcano volc = new Volcano(scan.nextInt(), scan.nextInt()); volcanoes.add(volc); } //System.out.println(calcPath(volcanoes)); //printVolcs(volcanoes); sortVolcs(volcanoes); //printVolcs(volcanoes); System.out.println(calcPath(volcanoes)); } }