import java.io.*;
import java.util.*;
public class Main {
private static class Read {
StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
public int read() throws IOException {
st.nextToken();
return (int) st.nval;
}
}
public static void main(String[] args) throws IOException {
Read rd = new Read();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
TreeSet<Integer> ts = new TreeSet<>();
int n = rd.read();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
int a = rd.read();
if (i == 0) {
arr[0] = Math.abs(a);
} else {
Integer floor = ts.floor(a);
Integer ceiling = ts.ceiling(a);
if (Objects.equals(floor, ceiling)) {
arr[i] = 0;
} else if (floor != null && ceiling != null) {
int num1 = Math.abs(a - floor);
int num2 = Math.abs(ceiling - a);
arr[i] = Math.min(num1, num2);
} else if (floor != null) {
arr[i] = Math.abs(a - floor);
} else {
arr[i] = Math.abs(ceiling - a);
}
}
ts.add(a);
}
ts = null;
int result = 0;
for (int elem : arr) {
result += elem;
}
pw.print(result);
pw.close();
}
}