import java.util.*;

public class Main {
	static class Node {
		Node parent;
		int value;
		Node right, left;

		Node(int value) {
			this.value = value;
			this.parent = null;
			this.right = null;
			this.left = null;
		}

		/**
		 * 値を追加して，rootのnodeを返す
		 * 
		 * @param value
		 * @return
		 */
		Node add(int value) {
			if (this.value >= value) {
				// 自分の親にする
				Node insert = new Node(value);
				Node parent = this.parent;
				if (parent != null) {
					parent.right = insert;
					insert.parent = parent;
				}
				insert.left = this;
				this.parent = insert;
				return parent;
			} else {
				// 右に挿入
				if (this.right == null) {
					this.right = new Node(value);
					this.right.parent = this;
					return this;
				} else {
					// 右の子として挿入
					this.right.add(value);
					return this;
				}
			}
		}

		Node getRoot() {
			if (this.parent == null) {
				return this;
			} else {
				return this.parent.getRoot();
			}
		}

		long getCost(long base) {
			if (this.left == null && this.right == null) {
				return this.value - base;
			} else if (this.left != null && this.right != null) {
				long result = 0;
				result += this.left.getCost(this.value);
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			} else if (this.left != null) {
				// leftのみ
				long result = 0;
				result += this.left.getCost((this.value));
				result += this.value - base;
				return result;
			} else {
				// rightのみ
				long result = 0;
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			}
		}
	}

	int N;
	int[] h;
	List<Deque<Integer>> sections;

	Main() {
		Scanner in = new Scanner(System.in);
		N = Integer.parseInt(in.nextLine());
		String[] tokens = in.nextLine().split(" ");
		h = new int[N];
		for (int i = 0; i < N; ++i) {
			h[i] = Integer.parseInt(tokens[i]);
		}
		in.close();
	}

	Node genTree(int[] h) {
		// 最初のnodeを作る
		Node root = new Node(h[0]);
		for (int i = 1; i < h.length; ++i) {
			root.add(h[i]);
			root = root.getRoot();
		}
		return root;
	}

	public static void main(String[] args) {
		Main ins = new Main();
		Node root = ins.genTree(ins.h);
		System.out.println(root.getCost(0));
	}

} import java.util.*; util.* java public class Main {
	static class Node {
		Node parent;
		int value;
		Node right, left;

		Node(int value) {
			this.value = value;
			this.parent = null;
			this.right = null;
			this.left = null;
		}

		/**
		 * 値を追加して，rootのnodeを返す
		 * 
		 * @param value
		 * @return
		 */
		Node add(int value) {
			if (this.value >= value) {
				// 自分の親にする
				Node insert = new Node(value);
				Node parent = this.parent;
				if (parent != null) {
					parent.right = insert;
					insert.parent = parent;
				}
				insert.left = this;
				this.parent = insert;
				return parent;
			} else {
				// 右に挿入
				if (this.right == null) {
					this.right = new Node(value);
					this.right.parent = this;
					return this;
				} else {
					// 右の子として挿入
					this.right.add(value);
					return this;
				}
			}
		}

		Node getRoot() {
			if (this.parent == null) {
				return this;
			} else {
				return this.parent.getRoot();
			}
		}

		long getCost(long base) {
			if (this.left == null && this.right == null) {
				return this.value - base;
			} else if (this.left != null && this.right != null) {
				long result = 0;
				result += this.left.getCost(this.value);
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			} else if (this.left != null) {
				// leftのみ
				long result = 0;
				result += this.left.getCost((this.value));
				result += this.value - base;
				return result;
			} else {
				// rightのみ
				long result = 0;
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			}
		}
	}

	int N;
	int[] h;
	List<Deque<Integer>> sections;

	Main() {
		Scanner in = new Scanner(System.in);
		N = Integer.parseInt(in.nextLine());
		String[] tokens = in.nextLine().split(" ");
		h = new int[N];
		for (int i = 0; i < N; ++i) {
			h[i] = Integer.parseInt(tokens[i]);
		}
		in.close();
	}

	Node genTree(int[] h) {
		// 最初のnodeを作る
		Node root = new Node(h[0]);
		for (int i = 1; i < h.length; ++i) {
			root.add(h[i]);
			root = root.getRoot();
		}
		return root;
	}

	public static void main(String[] args) {
		Main ins = new Main();
		Node root = ins.genTree(ins.h);
		System.out.println(root.getCost(0));
	}

} Main int N; N int[] h; h List<Deque<Integer>> sections; sections Main() {
		Scanner in = new Scanner(System.in);
		N = Integer.parseInt(in.nextLine());
		String[] tokens = in.nextLine().split(" ");
		h = new int[N];
		for (int i = 0; i < N; ++i) {
			h[i] = Integer.parseInt(tokens[i]);
		}
		in.close();
	} Main {
		Scanner in = new Scanner(System.in);
		N = Integer.parseInt(in.nextLine());
		String[] tokens = in.nextLine().split(" ");
		h = new int[N];
		for (int i = 0; i < N; ++i) {
			h[i] = Integer.parseInt(tokens[i]);
		}
		in.close();
	} Scanner in = new Scanner(System.in); in new Scanner(System.in) N = Integer.parseInt(in.nextLine()) N Integer.parseInt(in.nextLine()) Integer.parseInt Integer in.nextLine() in.nextLine in String[] tokens = in.nextLine().split(" "); tokens in.nextLine().split(" ") in.nextLine().split in.nextLine() in.nextLine in " " h = new int[N] h new int[N] N for (int i = 0; i < N; ++i) {
			h[i] = Integer.parseInt(tokens[i]);
		} int i = 0; int i = 0; i 0 i < N i N ++i ++i i {
			h[i] = Integer.parseInt(tokens[i]);
		} {
			h[i] = Integer.parseInt(tokens[i]);
		} h[i] = Integer.parseInt(tokens[i]) h[i] h i Integer.parseInt(tokens[i]) Integer.parseInt Integer tokens[i] tokens i in.close() in.close in Node genTree(int[] h) {
		// 最初のnodeを作る
		Node root = new Node(h[0]);
		for (int i = 1; i < h.length; ++i) {
			root.add(h[i]);
			root = root.getRoot();
		}
		return root;
	} genTree {
		// 最初のnodeを作る
		Node root = new Node(h[0]);
		for (int i = 1; i < h.length; ++i) {
			root.add(h[i]);
			root = root.getRoot();
		}
		return root;
	} Node root = new Node(h[0]); root new Node(h[0]) for (int i = 1; i < h.length; ++i) {
			root.add(h[i]);
			root = root.getRoot();
		} int i = 1; int i = 1; i 1 i < h.length i h.length h h.length ++i ++i i {
			root.add(h[i]);
			root = root.getRoot();
		} {
			root.add(h[i]);
			root = root.getRoot();
		} root.add(h[i]) root.add root h[i] h i root = root.getRoot() root root.getRoot() root.getRoot root return root; root int[] h h public static void main(String[] args) {
		Main ins = new Main();
		Node root = ins.genTree(ins.h);
		System.out.println(root.getCost(0));
	} main {
		Main ins = new Main();
		Node root = ins.genTree(ins.h);
		System.out.println(root.getCost(0));
	} Main ins = new Main(); ins new Main() Node root = ins.genTree(ins.h); root ins.genTree(ins.h) ins.genTree ins ins.h ins ins.h System.out.println(root.getCost(0)) System.out.println System.out System System.out root.getCost(0) root.getCost root 0 String[] args args static class Node {
		Node parent;
		int value;
		Node right, left;

		Node(int value) {
			this.value = value;
			this.parent = null;
			this.right = null;
			this.left = null;
		}

		/**
		 * 値を追加して，rootのnodeを返す
		 * 
		 * @param value
		 * @return
		 */
		Node add(int value) {
			if (this.value >= value) {
				// 自分の親にする
				Node insert = new Node(value);
				Node parent = this.parent;
				if (parent != null) {
					parent.right = insert;
					insert.parent = parent;
				}
				insert.left = this;
				this.parent = insert;
				return parent;
			} else {
				// 右に挿入
				if (this.right == null) {
					this.right = new Node(value);
					this.right.parent = this;
					return this;
				} else {
					// 右の子として挿入
					this.right.add(value);
					return this;
				}
			}
		}

		Node getRoot() {
			if (this.parent == null) {
				return this;
			} else {
				return this.parent.getRoot();
			}
		}

		long getCost(long base) {
			if (this.left == null && this.right == null) {
				return this.value - base;
			} else if (this.left != null && this.right != null) {
				long result = 0;
				result += this.left.getCost(this.value);
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			} else if (this.left != null) {
				// leftのみ
				long result = 0;
				result += this.left.getCost((this.value));
				result += this.value - base;
				return result;
			} else {
				// rightのみ
				long result = 0;
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			}
		}
	} Node Node parent; parent int value; value Node right right left; left Node(int value) {
			this.value = value;
			this.parent = null;
			this.right = null;
			this.left = null;
		} Node {
			this.value = value;
			this.parent = null;
			this.right = null;
			this.left = null;
		} this.value = value this.value this this.value value this.parent = null this.parent this this.parent null this.right = null this.right this this.right null this.left = null this.left this this.left null int value value /**
		 * 値を追加して，rootのnodeを返す
		 * 
		 * @param value
		 * @return
		 */
		Node add(int value) {
			if (this.value >= value) {
				// 自分の親にする
				Node insert = new Node(value);
				Node parent = this.parent;
				if (parent != null) {
					parent.right = insert;
					insert.parent = parent;
				}
				insert.left = this;
				this.parent = insert;
				return parent;
			} else {
				// 右に挿入
				if (this.right == null) {
					this.right = new Node(value);
					this.right.parent = this;
					return this;
				} else {
					// 右の子として挿入
					this.right.add(value);
					return this;
				}
			}
		} add {
			if (this.value >= value) {
				// 自分の親にする
				Node insert = new Node(value);
				Node parent = this.parent;
				if (parent != null) {
					parent.right = insert;
					insert.parent = parent;
				}
				insert.left = this;
				this.parent = insert;
				return parent;
			} else {
				// 右に挿入
				if (this.right == null) {
					this.right = new Node(value);
					this.right.parent = this;
					return this;
				} else {
					// 右の子として挿入
					this.right.add(value);
					return this;
				}
			}
		} if (this.value >= value) {
				// 自分の親にする
				Node insert = new Node(value);
				Node parent = this.parent;
				if (parent != null) {
					parent.right = insert;
					insert.parent = parent;
				}
				insert.left = this;
				this.parent = insert;
				return parent;
			} else {
				// 右に挿入
				if (this.right == null) {
					this.right = new Node(value);
					this.right.parent = this;
					return this;
				} else {
					// 右の子として挿入
					this.right.add(value);
					return this;
				}
			} this.value >= value this.value this this.value value {
				// 自分の親にする
				Node insert = new Node(value);
				Node parent = this.parent;
				if (parent != null) {
					parent.right = insert;
					insert.parent = parent;
				}
				insert.left = this;
				this.parent = insert;
				return parent;
			} Node insert = new Node(value); insert new Node(value) Node parent = this.parent; parent this.parent this this.parent if (parent != null) {
					parent.right = insert;
					insert.parent = parent;
				} parent != null parent null {
					parent.right = insert;
					insert.parent = parent;
				} parent.right = insert parent.right parent parent.right insert insert.parent = parent insert.parent insert insert.parent parent insert.left = this insert.left insert insert.left this this.parent = insert this.parent this this.parent insert return parent; parent {
				// 右に挿入
				if (this.right == null) {
					this.right = new Node(value);
					this.right.parent = this;
					return this;
				} else {
					// 右の子として挿入
					this.right.add(value);
					return this;
				}
			} if (this.right == null) {
					this.right = new Node(value);
					this.right.parent = this;
					return this;
				} else {
					// 右の子として挿入
					this.right.add(value);
					return this;
				} this.right == null this.right this this.right null {
					this.right = new Node(value);
					this.right.parent = this;
					return this;
				} this.right = new Node(value) this.right this this.right new Node(value) this.right.parent = this this.right.parent this.right this this.right this.right.parent this return this; this {
					// 右の子として挿入
					this.right.add(value);
					return this;
				} this.right.add(value) this.right.add this.right this this.right value return this; this int value value Node getRoot() {
			if (this.parent == null) {
				return this;
			} else {
				return this.parent.getRoot();
			}
		} getRoot {
			if (this.parent == null) {
				return this;
			} else {
				return this.parent.getRoot();
			}
		} if (this.parent == null) {
				return this;
			} else {
				return this.parent.getRoot();
			} this.parent == null this.parent this this.parent null {
				return this;
			} return this; this {
				return this.parent.getRoot();
			} return this.parent.getRoot(); this.parent.getRoot() this.parent.getRoot this.parent this this.parent long getCost(long base) {
			if (this.left == null && this.right == null) {
				return this.value - base;
			} else if (this.left != null && this.right != null) {
				long result = 0;
				result += this.left.getCost(this.value);
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			} else if (this.left != null) {
				// leftのみ
				long result = 0;
				result += this.left.getCost((this.value));
				result += this.value - base;
				return result;
			} else {
				// rightのみ
				long result = 0;
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			}
		} getCost {
			if (this.left == null && this.right == null) {
				return this.value - base;
			} else if (this.left != null && this.right != null) {
				long result = 0;
				result += this.left.getCost(this.value);
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			} else if (this.left != null) {
				// leftのみ
				long result = 0;
				result += this.left.getCost((this.value));
				result += this.value - base;
				return result;
			} else {
				// rightのみ
				long result = 0;
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			}
		} if (this.left == null && this.right == null) {
				return this.value - base;
			} else if (this.left != null && this.right != null) {
				long result = 0;
				result += this.left.getCost(this.value);
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			} else if (this.left != null) {
				// leftのみ
				long result = 0;
				result += this.left.getCost((this.value));
				result += this.value - base;
				return result;
			} else {
				// rightのみ
				long result = 0;
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			} this.left == null && this.right == null this.left == null this.left this this.left null this.right == null this.right this this.right null {
				return this.value - base;
			} return this.value - base; this.value - base this.value this this.value base if (this.left != null && this.right != null) {
				long result = 0;
				result += this.left.getCost(this.value);
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			} else if (this.left != null) {
				// leftのみ
				long result = 0;
				result += this.left.getCost((this.value));
				result += this.value - base;
				return result;
			} else {
				// rightのみ
				long result = 0;
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			} this.left != null && this.right != null this.left != null this.left this this.left null this.right != null this.right this this.right null {
				long result = 0;
				result += this.left.getCost(this.value);
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			} long result = 0; result 0 result += this.left.getCost(this.value) result this.left.getCost(this.value) this.left.getCost this.left this this.left this.value this this.value result += this.right.getCost(this.value) result this.right.getCost(this.value) this.right.getCost this.right this this.right this.value this this.value result += this.value - base result this.value - base this.value this this.value base return result; result if (this.left != null) {
				// leftのみ
				long result = 0;
				result += this.left.getCost((this.value));
				result += this.value - base;
				return result;
			} else {
				// rightのみ
				long result = 0;
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			} this.left != null this.left this this.left null {
				// leftのみ
				long result = 0;
				result += this.left.getCost((this.value));
				result += this.value - base;
				return result;
			} long result = 0; result 0 result += this.left.getCost((this.value)) result this.left.getCost((this.value)) this.left.getCost this.left this this.left (this.value) this this.value result += this.value - base result this.value - base this.value this this.value base return result; result {
				// rightのみ
				long result = 0;
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			} long result = 0; result 0 result += this.right.getCost(this.value) result this.right.getCost(this.value) this.right.getCost this.right this this.right this.value this this.value result += this.value - base result this.value - base this.value this this.value base return result; result long base base public class Main {
	static class Node {
		Node parent;
		int value;
		Node right, left;

		Node(int value) {
			this.value = value;
			this.parent = null;
			this.right = null;
			this.left = null;
		}

		/**
		 * 値を追加して，rootのnodeを返す
		 * 
		 * @param value
		 * @return
		 */
		Node add(int value) {
			if (this.value >= value) {
				// 自分の親にする
				Node insert = new Node(value);
				Node parent = this.parent;
				if (parent != null) {
					parent.right = insert;
					insert.parent = parent;
				}
				insert.left = this;
				this.parent = insert;
				return parent;
			} else {
				// 右に挿入
				if (this.right == null) {
					this.right = new Node(value);
					this.right.parent = this;
					return this;
				} else {
					// 右の子として挿入
					this.right.add(value);
					return this;
				}
			}
		}

		Node getRoot() {
			if (this.parent == null) {
				return this;
			} else {
				return this.parent.getRoot();
			}
		}

		long getCost(long base) {
			if (this.left == null && this.right == null) {
				return this.value - base;
			} else if (this.left != null && this.right != null) {
				long result = 0;
				result += this.left.getCost(this.value);
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			} else if (this.left != null) {
				// leftのみ
				long result = 0;
				result += this.left.getCost((this.value));
				result += this.value - base;
				return result;
			} else {
				// rightのみ
				long result = 0;
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			}
		}
	}

	int N;
	int[] h;
	List<Deque<Integer>> sections;

	Main() {
		Scanner in = new Scanner(System.in);
		N = Integer.parseInt(in.nextLine());
		String[] tokens = in.nextLine().split(" ");
		h = new int[N];
		for (int i = 0; i < N; ++i) {
			h[i] = Integer.parseInt(tokens[i]);
		}
		in.close();
	}

	Node genTree(int[] h) {
		// 最初のnodeを作る
		Node root = new Node(h[0]);
		for (int i = 1; i < h.length; ++i) {
			root.add(h[i]);
			root = root.getRoot();
		}
		return root;
	}

	public static void main(String[] args) {
		Main ins = new Main();
		Node root = ins.genTree(ins.h);
		System.out.println(root.getCost(0));
	}

} public class Main {
	static class Node {
		Node parent;
		int value;
		Node right, left;

		Node(int value) {
			this.value = value;
			this.parent = null;
			this.right = null;
			this.left = null;
		}

		/**
		 * 値を追加して，rootのnodeを返す
		 * 
		 * @param value
		 * @return
		 */
		Node add(int value) {
			if (this.value >= value) {
				// 自分の親にする
				Node insert = new Node(value);
				Node parent = this.parent;
				if (parent != null) {
					parent.right = insert;
					insert.parent = parent;
				}
				insert.left = this;
				this.parent = insert;
				return parent;
			} else {
				// 右に挿入
				if (this.right == null) {
					this.right = new Node(value);
					this.right.parent = this;
					return this;
				} else {
					// 右の子として挿入
					this.right.add(value);
					return this;
				}
			}
		}

		Node getRoot() {
			if (this.parent == null) {
				return this;
			} else {
				return this.parent.getRoot();
			}
		}

		long getCost(long base) {
			if (this.left == null && this.right == null) {
				return this.value - base;
			} else if (this.left != null && this.right != null) {
				long result = 0;
				result += this.left.getCost(this.value);
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			} else if (this.left != null) {
				// leftのみ
				long result = 0;
				result += this.left.getCost((this.value));
				result += this.value - base;
				return result;
			} else {
				// rightのみ
				long result = 0;
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			}
		}
	}

	int N;
	int[] h;
	List<Deque<Integer>> sections;

	Main() {
		Scanner in = new Scanner(System.in);
		N = Integer.parseInt(in.nextLine());
		String[] tokens = in.nextLine().split(" ");
		h = new int[N];
		for (int i = 0; i < N; ++i) {
			h[i] = Integer.parseInt(tokens[i]);
		}
		in.close();
	}

	Node genTree(int[] h) {
		// 最初のnodeを作る
		Node root = new Node(h[0]);
		for (int i = 1; i < h.length; ++i) {
			root.add(h[i]);
			root = root.getRoot();
		}
		return root;
	}

	public static void main(String[] args) {
		Main ins = new Main();
		Node root = ins.genTree(ins.h);
		System.out.println(root.getCost(0));
	}

} Main
import java.util.*;

public class Main {
	static class Node {
		Node parent;
		int value;
		Node right, left;

		Node(int value) {
			this.value = value;
			this.parent = null;
			this.right = null;
			this.left = null;
		}

		/**
		 * 値を追加して，rootのnodeを返す
		 * 
		 * @param value
		 * @return
		 */
		Node add(int value) {
			if (this.value >= value) {
				// 自分の親にする
				Node insert = new Node(value);
				Node parent = this.parent;
				if (parent != null) {
					parent.right = insert;
					insert.parent = parent;
				}
				insert.left = this;
				this.parent = insert;
				return parent;
			} else {
				// 右に挿入
				if (this.right == null) {
					this.right = new Node(value);
					this.right.parent = this;
					return this;
				} else {
					// 右の子として挿入
					this.right.add(value);
					return this;
				}
			}
		}

		Node getRoot() {
			if (this.parent == null) {
				return this;
			} else {
				return this.parent.getRoot();
			}
		}

		long getCost(long base) {
			if (this.left == null && this.right == null) {
				return this.value - base;
			} else if (this.left != null && this.right != null) {
				long result = 0;
				result += this.left.getCost(this.value);
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			} else if (this.left != null) {
				// leftのみ
				long result = 0;
				result += this.left.getCost((this.value));
				result += this.value - base;
				return result;
			} else {
				// rightのみ
				long result = 0;
				result += this.right.getCost(this.value);
				result += this.value - base;
				return result;
			}
		}
	}

	int N;
	int[] h;
	List<Deque<Integer>> sections;

	Main() {
		Scanner in = new Scanner(System.in);
		N = Integer.parseInt(in.nextLine());
		String[] tokens = in.nextLine().split(" ");
		h = new int[N];
		for (int i = 0; i < N; ++i) {
			h[i] = Integer.parseInt(tokens[i]);
		}
		in.close();
	}

	Node genTree(int[] h) {
		// 最初のnodeを作る
		Node root = new Node(h[0]);
		for (int i = 1; i < h.length; ++i) {
			root.add(h[i]);
			root = root.getRoot();
		}
		return root;
	}

	public static void main(String[] args) {
		Main ins = new Main();
		Node root = ins.genTree(ins.h);
		System.out.println(root.getCost(0));
	}

}