import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class B526 {
	static int ans = 0;
	public static void main(String args[])throws Exception {
		BufferedReader input = new BufferedReader(
				new InputStreamReader(System.in));

		int n = Integer.parseInt(input.readLine());
		String[] tokens = input.readLine().split(" ");
		int total = (int)Math.pow(2, n+1) - 1;
		int[] arr = new int[total];

		for(int i=1;i<total;i++)
			arr[i] = Integer.parseInt(tokens[i-1]);

		Tree tree = new Tree();
		tree.buildTree(arr, n);
		tree.minReq(tree.root, 0, tree.getMax(tree.root, 0));
		System.out.println(ans);
	}

	public static class Node {
		int weight;
		Node left;
		Node right;

		public Node(int weight) {
			this.weight = weight;
			left = null;
			right = null;
		}
	}

	public static class Tree {
		Node root;

		public Tree() {
			root = null;
		}

		public void buildTree(int[] arr, int n) {
			this.root = new Node(0);
			Queue<Node> q = new LinkedList<Node>();
			q.add(root);
			int index = 0;
			int total = (int)Math.pow(2, n) - 1;

			while(!q.isEmpty() && (index)<total) {
				Node node = q.poll();
				node.left = new Node(arr[2*index + 1]);
				node.right = new Node(arr[2*index + 2]);
				q.add(node.left);
				q.add(node.right);
				index++;
			}
		}

		public int getMax(Node node, int cost) {
			if(node == null)
				return cost;

			return Math.max(getMax(node.left, cost + node.weight), getMax(node.right, cost + node.weight));
		}

		public int minReq(Node node, int total, int req) {
//			System.out.println(node.weight);
			if(node.left == null && node.right == null) {
				return req - total - node.weight;
			} else {
				int leftreq = minReq(node.left, total + node.weight, req);
				int rightreq = minReq(node.right, total + node.weight, req);
				int min = Math.min(leftreq, rightreq);
				ans += (leftreq + rightreq - 2*min);
				return min;
			}
		}
	}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class B526 {
	static int ans = 0;
	public static void main(String args[])throws Exception {
		BufferedReader input = new BufferedReader(
				new InputStreamReader(System.in));

		int n = Integer.parseInt(input.readLine());
		String[] tokens = input.readLine().split(" ");
		int total = (int)Math.pow(2, n+1) - 1;
		int[] arr = new int[total];

		for(int i=1;i<total;i++)
			arr[i] = Integer.parseInt(tokens[i-1]);

		Tree tree = new Tree();
		tree.buildTree(arr, n);

		System.out.println(tree.minReq(tree.root, 0, tree.getMax(tree.root, 0)));
	}

	public static class Node {
		int weight;
		Node left;
		Node right;

		public Node(int weight) {
			this.weight = weight;
			left = null;
			right = null;
		}
	}

	public static class Tree {
		Node root;

		public Tree() {
			root = null;
		}

		public void buildTree(int[] arr, int n) {
			this.root = new Node(0);
			Queue<Node> q = new LinkedList<Node>();
			q.add(root);
			int index = 0;
			int total = (int)Math.pow(2, n) - 1;

			while(!q.isEmpty() && (index)<total) {
				Node node = q.poll();
				node.left = new Node(arr[2*index + 1]);
				node.right = new Node(arr[2*index + 2]);
				q.add(node.left);
				q.add(node.right);
				index++;
			}
		}

		public int getMax(Node node, int cost) {
			if(node == null)
				return cost;

			return Math.max(getMax(node.left, cost + node.weight), getMax(node.right, cost + node.weight));
		}

		public int minReq(Node node, int total, int req) {
//			System.out.println(node.weight);
			if(node.left == null && node.right == null) {
				return req - total;
			} else {
				int leftreq = minReq(node.left, total + node.weight, req);
				int rightreq = minReq(node.right, total + node.weight, req);
				int min = Math.min(leftreq, rightreq);
				ans += (leftreq + rightreq - 2*min);
				return min;
			}
		}
	}
}


import java.io.*;
import java.util.*;

public class CF526B {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		int m = 1 << n + 1;
		StringTokenizer st = new StringTokenizer(br.readLine());
		int[] aa = new int[m];
		for (int i = 2; i < m; i++)
			aa[i] = Integer.parseInt(st.nextToken());
		int[] bb = new int[m];
		for (int i = m - 1; i >= 2; i--)
			bb[i / 2] = Math.max(bb[i / 2], aa[i] + bb[i]);
		int cnt = 0;
		for (int i = m - 1; i >= 2; i--)
			cnt += bb[i / 2] - aa[i] - bb[i];
		System.out.println(cnt);
	}
}


import java.io.*;
import java.util.StringTokenizer;

/**
 * Created by watson on 04.04.15.
 */
public class A {
    FastScanner in;
    PrintWriter out;
    int[] a, b, c;
    int n, k;
    public void solve() throws IOException {
        k = in.nextInt();
        n = (int) Math.pow(2, k + 1) - 1;
        a = new int[n];
        b = new int[n];
        c = new int[n];
        for (int i = 1; i < n; i++) {
            a[i] = in.nextInt();
        }
        int max = -1;
        int imax = -1;
        dfs(0);
        int j = 0;
        long ans = 0;
        for (int i = 1; i < n; i++) {
            if (i % 2 == 1) {
                ans += Math.max(0, a[i] + b[i] - (a[i + 1] + b[i + 1]));
            } else {
                ans += Math.max(0, a[i] + b[i] - (a[i - 1] + b[i - 1]));
            }
        }
        System.out.println(ans);
    }

    public int dfs(int i) {
        if (i >= n / 2) {
            return a[i];
        }
        int c1 = dfs(2 * i + 1);
        int c2 = dfs(2 * i + 2);
        if (c1 > c2) {
            b[i] = c1;
            c[i] = 0;
        } else {
            b[i] = c2;
            c[i] = 1;
        }
        return b[i];
    }

    public void run() {
        try {
            in = new FastScanner(System.in);
            out = new PrintWriter(System.out);

            solve();

            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public FastScanner(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in));
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
    }

    public static void main(String[] arg) {
        new A().run();
    }
}


import java.io.*;
import java.util.StringTokenizer;

/**
 * Created by watson on 04.04.15.
 */
public class A {
    FastScanner in;
    PrintWriter out;
    int[] a, b, c;
    int n, k;
    public void solve() throws IOException {
        k = in.nextInt();
        n = (int) Math.pow(2, k + 1) - 1;
        a = new int[n];
        b = new int[n];
        c = new int[n];
        for (int i = 1; i < n; i++) {
            a[i] = in.nextInt();
        }
        dfs(0);
        int j = 0;
        long ans = 0;
        for (int i = 1; i < n; i++) {
            if (i % 2 == 1) {
                ans += Math.max(0, a[i] + b[i] - (a[i + 1] + b[i + 1]));
            } else {
                ans += Math.max(0, a[i] + b[i] - (a[i - 1] + b[i - 1]));
            }
        }
        System.out.println(ans);
    }

    public int dfs(int i) {
        if (i >= n / 2) {
            return 0;
        }
        int c1 = dfs(2 * i + 1) + a[2 * i + 1];
        int c2 = dfs(2 * i + 2) + a[2 * i + 2];
        if (c1 > c2) {
            b[i] = c1;
            c[i] = 0;
        } else {
            b[i] = c2;
            c[i] = 1;
        }
        return b[i];
    }

    public void run() {
        try {
            in = new FastScanner(System.in);
            out = new PrintWriter(System.out);

            solve();

            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public FastScanner(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in));
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
    }

    public static void main(String[] arg) {
        new A().run();
    }
}


import java.io.*;
import java.util.*;

public class Solution {

//	public static final double EPS = 1e-9;
//	public static final int MOD = 1000000009;
//	public static final double PI = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679;
//	public static final int DX[] = {1, -1, 0, 0};
//	public static final int DY[] = {0, 0, 1, -1};
//	public static final int sievesize = (int) (1e6);
//	public static boolean[] prime;

	public static void main(String[] args) throws IOException {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		Task solver = new Task();
		solver.solve(1, in, out);
		out.close();
	}

	static class Task {

		public void solve(int testNumber, InputReader in, OutputWriter out) throws IOException {
			int n = in.nextInt();
			int[] arr = new int[(int) Math.pow(2, n + 1)];
			for (int i = 2; i < arr.length; i++) arr[i] = in.nextInt();
			int i = (int) (Math.pow(2, n + 1) - 1);
			long ans = 0;
			while(i > 1) {
				ans += Math.abs(arr[i] - arr[i - 1]);
				if (arr[i - 1] < arr[i]) arr[i - 1] += (arr[i] - arr[i - 1]);
				else arr[i] += (arr[i - 1] - arr[i]);
				arr[i / 2] += arr[i];
				i -= 2;
			}
			out.println(ans, "\n");
		}

//		static class DSU {
//			int[] par, size;
//
//			public DSU(int n) {
//				par = new int[n];
//				size = new int[n];
//				init(n);
//			}
//
//			public void init(int n) {
//				for (int i = 0; i < n; i++) {
//					par[i] = i;
//					size[i] = 1;
//				}
//			}
//
//			public int find(int x) {
//				if (par[x] != x) return par[x] = find(par[x]);
//				return par[x];
//			}
//
//			public boolean same(int x, int y) {
//				return find(x) == find(y);
//			}
//
//			public void union(int x, int y) {
//				x = find(x);
//				y = find(y);
//				if (x == y) return;
//				par[x] = y;
//				size[y] += size[x];
//			}
//
//			public int getSize(int x) {
//				return size[find(x)];
//			}
//		}

//		static class Node {
//			String name;
//			LinkedList<Node> list;
//
//			public Node(String name) {
//				this.name = name;
//				list = new LinkedList<>();
//			}
//		}

//		static class Pair<T1, T2> {
//	      	T1 first;
//	      	T2 second;
//
//		    public Pair(T1 first, T2 second) {
//	    		this.first = first;
//				this.second = second;
//			}
//	    }

//		public void sieve() {
//			prime = new boolean[sievesize + 1];
//			Arrays.fill(prime, true);
//		    for(int x = 2; x * x < sievesize; x++) {
//		    	if(prime[x])
//		    		for(int y = x * 2; y < sievesize; y += x)
//		    			prime[y] = false;
//		    }
//		}

//		public int[] shuffle(int arr[], Random gen) {
//			for (int i = arr.length - 1; i > 0; i--) {
//	            int index = gen.nextInt(i + 1);
//	            int temp = arr[index];
//	            arr[index] = arr[i];
//	            arr[i] = temp;
//			}
//			return arr;
//		}

//		public boolean triangle(long a, long b, long c) {
//		    return ((a+b) > c && (b+c) >a && (a+c) > b);
//		}

//		public boolean isPerfectSquare(long n) {
//			if (n < 0) return false;
//		    long test = (long) (Math.sqrt(n) + 0.5);
//		    return test*test == n;
//		}

//		public int isnum(char s) {
//			if(s=='1' || s=='2' || s=='3' || s=='4' || s=='5' || s=='6' || s=='7' || s=='8' || s=='9' || s=='0')  return 1;
//			return 0;
//		}

//		public long nextPowerOf2(long n) {
//		    n--;
//		    n |= n >> 1;
//		    n |= n >> 2;
//		    n |= n >> 4;
//		    n |= n >> 8;
//		    n |= n >> 16;
//		    n++;
//		    return n;
//		}

//		public int gcd(int a, int b) {
//		    int t;
//		    while (b > 0){
//		    	a = a % b;
//		    	t = a;
//		    	a = b;
//		    	b = t;
//		    }
//		    return a;
//		}

//		public int lcm(int x, int y) {
//			int a = Math.max(x, y);
//			while (true) {
//				if (a % x == 0 && a % y == 0) return a;
//				a++;
//			}
//		}

//		public static int findGcdArray(int[] arr) {
//			int min = Integer.MAX_VALUE;
//			for (int i = 0; i < arr.length; i++)
//				min = Math.min(min, arr[i]);
//			while (min > 1) {
//				int i = 0, s = 0;
//				while (i < arr.length) {
//					s += arr[i] % min;
//					i++;
//				}
//				if (s == 0) return min;
//				min--;
//			}
//			return 1;
//		}
	}

	static class InputReader {
		public BufferedReader reader;
		public StringTokenizer tokenizer;

		public InputReader(InputStream stream) {
			reader = new BufferedReader(new InputStreamReader(stream), 32768);
			tokenizer = null;
		}

		private void tokenize() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
		}

		public String next() {
			tokenize();
			return tokenizer.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public char nextChar() {
			return next().charAt(0);
		}

		public float nextFloat() {
			return Float.parseFloat(next());
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

		public int[] nextIntArray(int size) {
			tokenize();
			int[] arr=new int[size];
			for(int i = 0; i < size; i++) {
				tokenize();
				arr[i] = Integer.parseInt(tokenizer.nextToken());
			}
			return arr;
		}

		public long[] nextLongArray(int size) {
			tokenize();
			long[] arr=new long[size];
			for(int i = 0; i < size; i++) {
				tokenize();
				arr[i] = Long.parseLong(tokenizer.nextToken());
			}
			return arr;
		}

		public float[] nextFloatArray(int size) {
			tokenize();
			float[] arr=new float[size];
			for(int i = 0; i < size; i++) {
				tokenize();
				arr[i] = Float.parseFloat(tokenizer.nextToken());
			}
			return arr;
		}

		public double[] nextDoubleArray(int size) {
			tokenize();
			double[] arr=new double[size];
			for(int i = 0; i < size; i++) {
				tokenize();
				arr[i] = Double.parseDouble(tokenizer.nextToken());
			}
			return arr;
		}

		public String[] nextStringArray(int size) throws IOException {
			String[] arr=new String[size];
			for(int i = 0; i < size; i++)
				arr[i] = reader.readLine();
			return arr;
		}

		public char[] nextCharArray(int size) throws IOException {
			return reader.readLine().toCharArray();
		}
	}

	static class OutputWriter {
		public PrintWriter pw;

		public OutputWriter(OutputStream stream) {
			pw = new PrintWriter(stream);
		}

		public void println(Object... obj) {
			if (obj.length == 0) pw.println();
			else {
				String delimiter = (String) obj[obj.length - 1];
				for (int i = 0 ; i < obj.length - 2; i++) {
					pw.print(obj[i]);
					pw.print(delimiter);
				}
				pw.println(obj[obj.length - 2]);
			}
		}

		public void close() {
			pw.close();
		}

		public void flush() {
			pw.flush();
		}

		public void printStringArray(String[] arr) {
			for(int i = 0; i < arr.length; i++)
				pw.println(arr[i]);
		}

		public void printBooleanArray(boolean[] arr, int a , int b) {
			for(int i = a; i < b; i++)
				pw.print(arr[i] + " ");
		}

		public void printIntArray(int[] arr, int a , int b) {
			for(int i = a; i < b; i++)
				pw.print(arr[i] + " ");
		}

		public void printLongArray(long[] arr, int a , int b) {
			for(int i = a; i < b; i++)
				pw.print(arr[i] + " ");
		}

		public void printCharArray(char[] arr, int a , int b) {
			for(int i = a; i < b; i++)
				pw.print(arr[i] + " ");
		}

		public void printFloatArray(float[] arr, int a , int b) {
			for(int i = a; i < b; i++)
				pw.print(arr[i] + " ");
		}

		public void printDoubleArray(double[] arr, int a , int b) {
			for(int i = a; i < b; i++)
				pw.print(arr[i] + " ");
		}

		public void printIntArrayList(ArrayList<Integer> arr, int a , int b) {
			for(int i = a; i < b; i++)
				pw.print(arr.get(i) + " ");
		}

		public void printLongArrayList(ArrayList<Long> arr, int a , int b) {
			for(int i = a; i < b; i++)
				pw.print(arr.get(i) + " ");
		}
	}
}


import java.util.*;
import java.io.*;

public class Main implements Runnable {

	public void solve() throws IOException {
		int n = nextInt();
                int[] a = new int[1 <<(n+1)];
                for(int i = 2; i < a.length; i++)
                    a[i] = nextInt();

                int answer = 0;
                for(int i = (1 << n) - 1; i >= 1; i--){
                    int x = a[2 * i];
                    int y = a[2 * i + 1];
                    a[i] += Math.max(x, y);
                    answer += Math.max(x, y) * 2 - x - y;
                }

                out.println(answer);
	}

	//-----------------------------------------------------------
	public static void main(String[] args) {
		new Main().run();
	}

        public void debug(Object... arr){
            System.out.println(Arrays.deepToString(arr));
        }

        public void print1Int(int[] a){
                for(int i = 0; i < a.length; i++)
                        System.out.print(a[i] + " ");
                System.out.println();
        }

        public void print2Int(int[][] a){
                for(int i = 0; i < a.length; i++){
                        for(int j = 0; j < a[0].length; j++){
                                System.out.print(a[i][j] + " ");
                        }
                        System.out.println();
                }
        }

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
                        out = new PrintWriter(System.out);
			tok = null;
			solve();
			in.close();
                        out.close();
		} catch (IOException e) {
			System.exit(0);
		}
	}

	public String nextToken() throws IOException {
		while (tok == null || !tok.hasMoreTokens()) {
			tok = new StringTokenizer(in.readLine());
		}
		return tok.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
        PrintWriter out;
	BufferedReader in;
	StringTokenizer tok;
}


import java.util.*;
import java.io.*;

public class Main implements Runnable {

	public void solve() throws IOException {
		int n = nextInt();
                int[] a = new int[1 <<(n+1)];
                for(int i = 2; i < a.length; i++)
                    a[i] = nextInt();

                int answer = 0;
                for(int i = 1 << n - 1; i >= 1; i--){
                    int x = a[2 * i];
                    int y = a[2 * i + 1];
                    a[i] += Math.max(x, y);
                    answer += Math.max(x, y) * 2 - x - y;
                }

                out.println(answer);
	}

	//-----------------------------------------------------------
	public static void main(String[] args) {
		new Main().run();
	}

        public void debug(Object... arr){
            System.out.println(Arrays.deepToString(arr));
        }

        public void print1Int(int[] a){
                for(int i = 0; i < a.length; i++)
                        System.out.print(a[i] + " ");
                System.out.println();
        }

        public void print2Int(int[][] a){
                for(int i = 0; i < a.length; i++){
                        for(int j = 0; j < a[0].length; j++){
                                System.out.print(a[i][j] + " ");
                        }
                        System.out.println();
                }
        }

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
                        out = new PrintWriter(System.out);
			tok = null;
			solve();
			in.close();
                        out.close();
		} catch (IOException e) {
			System.exit(0);
		}
	}

	public String nextToken() throws IOException {
		while (tok == null || !tok.hasMoreTokens()) {
			tok = new StringTokenizer(in.readLine());
		}
		return tok.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
        PrintWriter out;
	BufferedReader in;
	StringTokenizer tok;
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Om_Nom_and_Dark_Park {
    public static long a[]=new long[123456];
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new BufferedReader(new InputStreamReader(System.in)));
        int n=Integer.parseInt(br.readLine());
        String str[]=br.readLine().split(" ");
        for(int i=2;i<=(int) (Math.pow(2,(n+1))-1);i++) {
            a[i] = Long.parseLong(str[i - 2]);
            //System.out.println(i+" "+a[i]);
        }
        long ans=0;
        for(int i=(int) (Math.pow(2,n)-1);i>=1;i--)
        {
            long x=a[2*i];
            long y=a[(2*i)+1];
            a[i]+=Math.max(x,y);
         ans+=Math.max(x,y)-Math.min(x,y);
         //System.out.println("x: "+x+" y: "+y+" ans: "+ans);
        }
        System.out.println(ans);
    }
}


import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskB {
    static int[] leaves;
    static int n;
    static int[] a;

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        n = in.nextInt();
        int len = (int) Math.pow(2, n + 1) - 2;
        a = in.nextAi(len);
        leaves = new int[1 << n];
        dfs(1, 1);

        out.println(ans);
    }

    int ans = 0;

    private int dfs(int level, int curNode) {
        int next = curNode << 1;
        int left = a[next - 2];
        int right = a[next - 1];
        if (level == n) {
            ans += Math.abs(left - right);
            return Math.max(left, right);
        }
        left += dfs(level + 1, next);
        right += dfs(level + 1, next + 1);
        ans += Math.abs(left - right);
        return -1;
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public int[] nextAi(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextInt();
        }
        return a;
    }

}


import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskB {
    static int[] leaves;
    static int n;
    static int[] a;

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        n = in.nextInt();
        int len = (int) Math.pow(2, n + 1) - 2;
        a = in.nextAi(len);
        leaves = new int[1 << n];
        dfs(1, 1);

        out.println(ans);
    }

    int ans = 0;

    private int dfs(int level, int curNode) {
        int next = curNode << 1;
        int left = a[next - 2];
        int right = a[next - 1];
        if (level == n) {
            ans += Math.abs(left - right);
            return Math.max(left, right);
        }
        left += dfs(level + 1, next);
        right += dfs(level + 1, next + 1);
        ans += Math.abs(left - right);
        return 0;
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public int[] nextAi(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextInt();
        }
        return a;
    }

}


import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskB {
    static int[] leaves;
    static int n;
    static int[] a;

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        n = in.nextInt();
        int len = (int) Math.pow(2, n + 1) - 2;
        a = in.nextAi(len);
        leaves = new int[1 << n];
        dfs(1, 1);

        out.println(ans);
    }

    int ans = 0;

    private int dfs(int level, int curNode) {
        int next = curNode << 1;
        int left = a[next - 2];
        int right = a[next - 1];
        if (level == n) {
            ans += Math.abs(left - right);
            return Math.max(left, right);
        }
        left += dfs(level + 1, next);
        right += dfs(level + 1, next + 1);
        ans += Math.abs(left - right);
        return Math.max(left, right);
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public int[] nextAi(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextInt();
        }
        return a;
    }

}


import java.util.Scanner;

public class Main {
	static int level = 0;

	static int total = 0;

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		level = n;

		int x = (int) (Math.pow(n, 2 + 1) - 1);
		Node root = new Node(0);
		bfs(in, root, 1);

		bfs1(root, 0);

		System.out.println(total);

	}

	public static void bfs1(Node root, int value) {
		if (root.l == null && root.r == null) {
			return;
		}
		bfs1(root.l, 0);
		bfs1(root.r, 0);
		root.s = root.l.s + root.l.v;
		total += Math.abs(root.l.s + root.l.v - root.r.s - root.r.v);
	}

	public static void bfs(Scanner in, Node root, int lev) {
		if (lev > level) {
			return;
		}
		root.l = new Node(in.nextInt());
		root.r = new Node(in.nextInt());
		bfs(in, root.l, lev + 1);
		bfs(in, root.r, lev + 1);
	}
}

class Node {
	Node(int v) {
		this.v = v;
	}

	public int v;

	public int s = 0;

	public int b = 0;

	public Node l = null;

	public Node r = null;
}


import java.util.Scanner;

public class Main {
	static int level = 0;

	static int total = 0;

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		level = n;

		Node root = new Node(0);
		bfs(in, root, 1);

		bfs1(root);

		System.out.println(total);

	}

	public static void bfs1(Node root) {
		if (root.l == null && root.r == null) {
			return;
		}
		bfs1(root.l);
		bfs1(root.r);
		int temp = Math.abs(root.l.s + root.l.v - root.r.s - root.r.v);
		root.s = (root.l.s + root.l.v + root.r.s + root.r.v + temp) / 2;
		total += temp;
	}

	public static void bfs(Scanner in, Node root, int lev) {
		if (lev > level) {
			return;
		}
		root.l = new Node(in.nextInt());
		root.r = new Node(in.nextInt());
		bfs(in, root.l, lev + 1);
		bfs(in, root.r, lev + 1);
	}
}

class Node {
	Node(int v) {
		this.v = v;
	}

	public int v;

	public int s = 0;

	public int b = 0;

	public Node l = null;

	public Node r = null;
}


import java.util.Scanner;

public class Main {
	static int level = 0;

	static int total = 0;

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		level = n;

		Node root = new Node(0);
		bfs(in, root, 1);

		bfs1(root);

		System.out.println(total);

	}

	public static void bfs1(Node root) {
		if (root.l == null && root.r == null && true) {
			return;
		}
		bfs1(root.l);
		bfs1(root.r);
		int temp = Math.abs(root.l.v - root.r.v);
		root.v = Math.max(root.l.v, root.r.v) + root.v;
		total += temp;
	}

	public static void bfs(Scanner in, Node root, int lev) {
		if (lev > level) {
			return;
		}
		root.l = new Node(in.nextInt());
		root.r = new Node(in.nextInt());
		bfs(in, root.l, lev + 1);
		bfs(in, root.r, lev + 1);
	}
}

class Node {
	Node(int v) {
		this.v = v;
	}

	public int v;

	public int s = 0;

	public Node l = null;

	public Node r = null;
}


import java.util.Scanner;

public class Main {
	static int level = 0;

	static int total = 0;

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		level = n;

		Node root = new Node(0);
		bfs(in, root, 1);

		bfs1(root);

		System.out.println(total);

	}

	public static void bfs1(Node root) {
		if (root.l == null && root.r == null) {
			return;
		}
		bfs1(root.l);
		bfs1(root.r);
		int temp = Math.abs(root.l.v - root.r.v);
		root.v = Math.max(root.l.v, root.r.v) + root.v;
		total += temp;
	}

	public static void bfs(Scanner in, Node root, int lev) {
		if (lev > level) {
			return;
		}
		root.l = new Node(in.nextInt());
		root.r = new Node(in.nextInt());
		bfs(in, root.l, lev + 1);
		bfs(in, root.r, lev + 1);
	}
}

class Node {
	Node(int v) {
		this.v = v;
	}

	public int v;

	public int s = 0;

	public Node l = null;

	public Node r = null;
}


import java.util.Scanner;

public class Main {
	static int level = 0;

	static int total = 0;

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		level = n;

		Node root = new Node(0);
		Node[] queue = new Node[3000];
		queue[0] = root;
		int front = 0;
		int end = 1;
		int scans = 0;
		while (scans < Math.pow(2, n + 1) - 2) {
			Node c = queue[front];
			c.l = new Node(in.nextInt());
			c.r = new Node(in.nextInt());
			scans += 2;
			queue[end++] = c.l;
			queue[end++] = c.r;
			front++;
		}
		bfs1(root);

		System.out.println(total);

	}

	public static void bfs1(Node root) {
		if (root.l == null && root.r == null) {
			return;
		}
		bfs1(root.l);
		bfs1(root.r);
		int temp = Math.abs(root.l.v - root.r.v);
		root.v = Math.max(root.l.v, root.r.v) + root.v;
		total += temp;
	}
}

class Node {
	Node(int v) {
		this.v = v;
	}

	public int v;

	public int s = 0;

	public Node l = null;

	public Node r = null;
}


import java.util.*;

public class Path {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int depth = in.nextInt();
        int currentDepth = 0;
        int depthCount = 2;
        int[] data = new int[(int)Math.pow(2, depth)];

        int max = 0;

        while (currentDepth < depth) {
            for (int i = 0; i < depthCount; i++) {
                int value = in.nextInt();
                int start = data.length / depthCount;

                for (int j = i * start; j < (i + 1) * start; j++) {
                    data[j] += value;
                    if (data[j] > max)
                        max = data[j];
                }
            }
            depthCount *= 2;
            currentDepth++;
        }

        currentDepth = 0;
        depthCount = 2;
        int count = 0;

        while (currentDepth < depth) {
            for (int i = 0; i < depthCount; i++) {
                int start = data.length / depthCount;
                int minDiff = Integer.MAX_VALUE;
                for (int j = i * start; j < (i + 1) * start; j++) {
                    if (minDiff > max - data[j])
                        minDiff = max - data[j];
                }
                count += minDiff;
                for (int j = i * start; j < (i + 1) * start; j++)
                    data[j] += minDiff;
            }
            depthCount *= 2;
            currentDepth++;
        }

        System.out.println(count);
    }

}


import java.util.*;

public class OmNomAndDarkPark
{
	public static void main(String args[])
	{
		int n;
		Scanner s=new Scanner(System.in);
		n=s.nextInt();
		int num=(1<<(n+1));
		int[] a=new int[num];
		for(int i=2;i<num;i++)
		{
			a[i]=s.nextInt();
		}
		int sum=0;
		for(int i=(1<<n)-1;i>=1;i--)
		{
			sum=sum+Math.abs(a[2*i]-a[2*i+1]);
			a[i]=a[i]+Math.max(a[2*i],a[2*i+1]);
		}
		System.out.print(sum);
	}
}


import java.io.*;
import java.util.*;

import static java.lang.Math.*;

public class Main extends PrintWriter {
    BufferedReader in;
    StringTokenizer stok;
    final Random rand = new Random(31);
    final int inf = (int) 1e9;
    final long linf = (long) 1e18;

    void solve() throws IOException {
        int n = nextInt();
        int[] a = new int[(1 << (n + 1)) - 1];
        for (int i = 1; i < a.length; i++) {
            a[i] = nextInt();
        }
        long ans = 0;
        for (int i = (1 << n) - 2; i >= 0; i--) {
            int x = a[i * 2 + 1];
            int y = a[i * 2 + 2];
            ans += abs(x - y);
            a[i] += max(x, y);
        }
        println(ans);
    }

    void run() {
        try {
            solve();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(abs(-1));
        } finally {
            close();
        }
    }

    Main() throws IOException {
        super(System.out);
        in = new BufferedReader(new InputStreamReader(System.in));
    }

    Main(String filename) throws IOException {
        super("".equals(filename) ? "output.txt" : filename + ".out");
        in = new BufferedReader(new FileReader("".equals(filename) ? "input.txt" : filename + ".in"));
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }

    String next() throws IOException {
        while (stok == null || !stok.hasMoreTokens()) {
            String s = in.readLine();
            if (s == null) {
                return null;
            }
            stok = new StringTokenizer(s);
        }
        return stok.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    int[] nextIntArray(int len) throws IOException {
        int[] res = new int[len];
        for (int i = 0; i < len; i++) {
            res[i] = nextInt();
        }
        return res;
    }

    void shuffle(int[] a) {
        for (int i = 1; i < a.length; i++) {
            int x = rand.nextInt(i + 1);
            int _ = a[i];
            a[i] = a[x];
            a[x] = _;
        }
    }

    boolean nextPerm(int[] p) {
        for (int a = p.length - 2; a >= 0; --a)
            if (p[a] < p[a + 1])
                for (int b = p.length - 1; ; --b)
                    if (p[b] > p[a]) {
                        int t = p[a];
                        p[a] = p[b];
                        p[b] = t;
                        for (++a, b = p.length - 1; a < b; ++a, --b) {
                            t = p[a];
                            p[a] = p[b];
                            p[b] = t;
                        }
                        return true;
                    }
        return false;
    }

    <T> List<T>[] createAdjacencyList(int countVertex) {
        List<T>[] res = new List[countVertex];
        for (int i = 0; i < countVertex; i++) {
            res[i] = new ArrayList<T>();
        }
        return res;
    }

    class Pair<A extends Comparable<A>, B extends Comparable<B>> implements Comparable<Pair<A, B>> {
        A a;
        B b;

        public Pair(A a, B b) {
            this.a = a;
            this.b = b;
        }

        @Override
        public int compareTo(Pair<A, B> o) {
            int aa = a.compareTo(o.a);
            return aa == 0 ? b.compareTo(o.b) : aa;
        }
    }
}


import java.util.Scanner;

/**
 *
 * @author ADNAN
 */
public class SimpleTask {

    static int res = 0, n, matrix[][], max[];

    ;

    public static void main(String args[]) {

        Scanner scanner = new Scanner(System.in);

        n = scanner.nextInt();
        scanner.nextLine();

        matrix = new int[1 << (n + 1)][2];
        max = new int[1 << (n + 1)];
        max[0] = -1;

        for (int i = 1; i < 1 << n; i++) {
            matrix[i][i * 2 % 2] = scanner.nextInt();
            matrix[i][(i * 2 + 1) % 2] = scanner.nextInt();
        }
        scanner.nextLine();
        dfs(1);

        System.out.println(add(2, max[1]) + add(3, max[1]));

    }

    public static int dfs(int i) {
        if ((1 << n) <= i && i <= ((1 << (n + 1)) - 1)) {
            return max[i] = 0;
        }
        for (int j = 0; j < 2; j++) {
            max[i] = Math.max(max[i], matrix[i][j] + dfs(i * 2 + j));
        }
        return max[i];

    }

    public static int add(int i, int c) {
        if ((1 << n) <= i && i <= ((1 << (n + 1)) - 1)) {
            return c - matrix[i / 2][i % 2];
        }
        int amount = c - (max[i] + matrix[i / 2][i % 2]);
        if (amount > 0) {
            c -= (amount + matrix[i / 2][i % 2]);
            return amount + add(i * 2, c) + add(i * 2 + 1, c);
        } else {
            c -= matrix[i / 2][i % 2];
            return add(i * 2, c) + add(i * 2 + 1, c);
        }
    }

}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class Main {

	// split de array
	public static int[] readInts(String cad) {
		String read[] = cad.split(" ");
		int res[] = new int[read.length];
		for (int i = 0; i < read.length; i++) {
			res[i] = Integer.parseInt(read[i]);
		}
		return res;
	}

	public static class Node {
		int numLeft;
		int numRight;
		int index;
		Node left;
		Node right;

		public Node( int numLeft, int numRight, Node left, Node right, int index ) {
			this.numLeft = numLeft;
			this.numRight = numRight;
			this.left = left;
			this.right = right;
			this.index = index;
		}

	}

	public static int arr[];

	public static class Tree {
		public Node root;

		public void makeTree() {
			Queue<Node> q = new LinkedList<>();
			q.add(root);
			while (!q.isEmpty()) {
				Node t = q.poll();

				if (t.index * 2 < arr.length) {
					int a = t.index * 4;
					int b = ((t.index * 2) * 2) + 1;
					int a2 = ((t.index * 2) + 1) * 2;
					int b2 = (((t.index * 2) + 1) * 2) + 1;
					if (a < arr.length) {
						t.left = new Node(arr[a - 1], arr[b - 1], null, null,
								t.index * 2);
						t.right = new Node(arr[a2 - 1], arr[b2 - 1], null, null,
								t.index * 2 + 1);
						q.add(t.left);
						q.add(t.right);
					} else {

					}

				}
			}

		}
	}

	public static void main(String[] args) throws IOException {
		long inicio = System.currentTimeMillis();
		StringBuilder out = new StringBuilder();
		BufferedReader in;
		File archivo = new File("entrada");
		if (archivo.exists())
			in = new BufferedReader(new FileReader(archivo));
		else
			in = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(in.readLine());
		arr = readInts(in.readLine());
		int k = (int) Math.pow(2, n);
		long total = 0;
		int m = 0;
		for (int i = arr.length - 1; i >= 0;) {
			int index = i;
			int r = (int) Math.pow(2, n - 1);
			int cont = 0;

			if (i == arr.length - 1)
				while (cont < r) {
					int max = Math.max(arr[index], arr[index - 1]);
					if (arr[index] < arr[index - 1]) {
						total += Math.abs(arr[index] - arr[index - 1]);
						arr[index] += Math.abs(arr[index] - arr[index - 1]);
					} else {
						total += Math.abs(arr[index] - arr[index - 1]);
						arr[index - 1] += Math.abs(arr[index] - arr[index - 1]);
					}
					m = Math.max(m, max);

					index -= 2;
					i -= 2;
					cont++;
				}
			else {
				int m2 = 0;

				while (cont < r) {
					int a = arr[index]
							+ Math.max(arr[(index + 1) * 2], arr[(index + 1) * 2 + 1]);
					int b = arr[index - 1]
							+ Math.max(arr[(index) * 2], arr[(index) * 2 + 1]);
					if (a > b) {
						total += a - b;
					} else {
						total += b - a;
					}
					m2 = Math.max(a, b);
					index -= 2;
					i -= 2;
					cont++;
				}
				m = m2;
			}

			n--;
		}
		System.out.println(total);
		System.out.print(out);
		if (archivo.exists())
			System.out.println("Tiempo transcurrido : "
					+ (System.currentTimeMillis() - inicio) + " milisegundos.");
	}
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class Main {

	// split de array
	public static int[] readInts(String cad) {
		String read[] = cad.split(" ");
		int res[] = new int[read.length];
		for (int i = 0; i < read.length; i++) {
			res[i] = Integer.parseInt(read[i]);
		}
		return res;
	}

	public static class Node {
		int numLeft;
		int numRight;
		int index;
		Node left;
		Node right;

		public Node( int numLeft, int numRight, Node left, Node right, int index ) {
			this.numLeft = numLeft;
			this.numRight = numRight;
			this.left = left;
			this.right = right;
			this.index = index;
		}

	}

	public static int arr[];

	public static class Tree {
		public Node root;

		public void makeTree() {
			Queue<Node> q = new LinkedList<>();
			q.add(root);
			while (!q.isEmpty()) {
				Node t = q.poll();

				if (t.index * 2 < arr.length) {
					int a = t.index * 4;
					int b = ((t.index * 2) * 2) + 1;
					int a2 = ((t.index * 2) + 1) * 2;
					int b2 = (((t.index * 2) + 1) * 2) + 1;
					if (a < arr.length) {
						t.left = new Node(arr[a - 1], arr[b - 1], null, null,
								t.index * 2);
						t.right = new Node(arr[a2 - 1], arr[b2 - 1], null, null,
								t.index * 2 + 1);
						q.add(t.left);
						q.add(t.right);
					} else {

					}

				}
			}

		}
	}

	public static void main(String[] args) throws IOException {
		long inicio = System.currentTimeMillis();
		StringBuilder out = new StringBuilder();
		BufferedReader in;
		File archivo = new File("entrada");
		if (archivo.exists())
			in = new BufferedReader(new FileReader(archivo));
		else
			in = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(in.readLine());
		arr = readInts(in.readLine());
		int k = (int) Math.pow(2, n);
		long total = 0;
		for (int i = arr.length - 1; i >= 0;) {
			int index = i;
			int r = (int) Math.pow(2, n - 1);
			int cont = 0;

			if (i == arr.length - 1)
				while (cont < r) {
					int max = Math.max(arr[index], arr[index - 1]);
					if (arr[index] < arr[index - 1]) {
						total += Math.abs(arr[index] - arr[index - 1]);
						arr[index] += Math.abs(arr[index] - arr[index - 1]);
					} else {
						total += Math.abs(arr[index] - arr[index - 1]);
						arr[index - 1] += Math.abs(arr[index] - arr[index - 1]);
					}

					index -= 2;
					i -= 2;
					cont++;
				}
			else {
				int m2 = 0;

				while (cont < r) {
					int a = arr[index]
							+ Math.max(arr[(index + 1) * 2], arr[(index + 1) * 2 + 1]);
					int b = arr[index - 1]
							+ Math.max(arr[(index) * 2], arr[(index) * 2 + 1]);
					if (a > b) {
						arr[index - 1] += a - b;
						total += a - b;
					} else {
						arr[index] += b - a;
						total += b - a;
					}

					index -= 2;
					i -= 2;
					cont++;
				}
			}

			n--;
		}
		System.out.println(total);
		System.out.print(out);
		if (archivo.exists())
			System.out.println("Tiempo transcurrido : "
					+ (System.currentTimeMillis() - inicio) + " milisegundos.");
	}
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	// split de array
	public static int[] readInts(String cad) {
		String read[] = cad.split(" ");
		int res[] = new int[read.length + 2];
		for (int i = 0; i < read.length; i++) {
			res[i + 2] = Integer.parseInt(read[i]);
		}
		return res;
	}

	public static int total = 0;

	public static boolean visited[];

	public static int solve(int arr[], int i) {
		if (i * 2 >= arr.length)
			return 0;

		int a = solve(arr, i * 2) + arr[i * 2];
		int b = solve(arr, i * 2 + 1) + arr[i * 2 + 1];
		int max = Math.max(a, b);
		total += Math.abs(a - b);
		return max;
	}

	public static void main(String[] args) throws IOException {
		long inicio = System.currentTimeMillis();
		StringBuilder out = new StringBuilder();
		BufferedReader in;
		File archivo = new File("entrada");
		if (archivo.exists())
			in = new BufferedReader(new FileReader(archivo));
		else
			in = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(in.readLine());
		int arr[] = readInts(in.readLine());
		total = 0;
		int k = (int) (Math.pow(2, n) - 1);

		solve(arr, 1);

		System.out.println(total);

		System.out.print(out);
		if (archivo.exists())
			System.out.println("Tiempo transcurrido : "
					+ (System.currentTimeMillis() - inicio) + " milisegundos.");
	}
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class Main {

	// split de array
	public static int[] readInts(String cad) {
		String read[] = cad.split(" ");
		int res[] = new int[read.length];
		for (int i = 0; i < read.length; i++) {
			res[i] = Integer.parseInt(read[i]);
		}
		return res;
	}

	public static class Node {
		int numLeft;
		int numRight;
		int index;
		Node left;
		Node right;

		public Node( int numLeft, int numRight, Node left, Node right, int index ) {
			this.numLeft = numLeft;
			this.numRight = numRight;
			this.left = left;
			this.right = right;
			this.index = index;
		}

	}

	public static int arr[];

	public static class Tree {
		public Node root;

		public void makeTree() {
			Queue<Node> q = new LinkedList<>();
			q.add(root);
			while (!q.isEmpty()) {
				Node t = q.poll();

				if (t.index * 2 < arr.length) {
					int a = t.index * 4;
					int b = ((t.index * 2) * 2) + 1;
					int a2 = ((t.index * 2) + 1) * 2;
					int b2 = (((t.index * 2) + 1) * 2) + 1;
					if (a < arr.length) {
						t.left = new Node(arr[a - 1], arr[b - 1], null, null,
								t.index * 2);
						t.right = new Node(arr[a2 - 1], arr[b2 - 1], null, null,
								t.index * 2 + 1);
						q.add(t.left);
						q.add(t.right);
					} else {

					}

				}
			}

		}
	}

	public static void main(String[] args) throws IOException {
		long inicio = System.currentTimeMillis();
		StringBuilder out = new StringBuilder();
		BufferedReader in;
		File archivo = new File("entrada");
		if (archivo.exists())
			in = new BufferedReader(new FileReader(archivo));
		else
			in = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(in.readLine());
		arr = readInts(in.readLine());
		int k = (int) Math.pow(2, n);
		long total = 0;
		int m = 0;
		for (int i = arr.length - 1; i >= 0;) {
			int index = i;
			int r = (int) Math.pow(2, n - 1);
			int cont = 0;

			if (i == arr.length - 1)
				while (cont < r) {
					int max = Math.max(arr[index], arr[index - 1]);
					if (arr[index] < arr[index - 1]) {
						total += Math.abs(arr[index] - arr[index - 1]);
						arr[index] += Math.abs(arr[index] - arr[index - 1]);
					} else {
						total += Math.abs(arr[index] - arr[index - 1]);
						arr[index - 1] += Math.abs(arr[index] - arr[index - 1]);
					}
					m = Math.max(m, max);

					index -= 2;
					i -= 2;
					cont++;
				}
			else {
				int m2 = 0;

				while (cont < r) {
					int a = arr[index]
							+ Math.max(arr[(index + 1) * 2], arr[(index + 1) * 2 + 1]);
					int b = arr[index - 1]
							+ Math.max(arr[(index) * 2], arr[(index) * 2 + 1]);
					if (a > b) {
						arr[index] += a - b;
						total += a - b;
					} else {
						arr[index - 1] += a - b;
						total += b - a;
					}
					m2 = Math.max(a, b);
					index -= 2;
					i -= 2;
					cont++;
				}
				m = m2;
			}

			n--;
		}
		System.out.println(total);
		System.out.print(out);
		if (archivo.exists())
			System.out.println("Tiempo transcurrido : "
					+ (System.currentTimeMillis() - inicio) + " milisegundos.");
	}
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class Main {

	// split de array
	public static int[] readInts(String cad) {
		String read[] = cad.split(" ");
		int res[] = new int[read.length];
		for (int i = 0; i < read.length; i++) {
			res[i] = Integer.parseInt(read[i]);
		}
		return res;
	}

	public static class Node {
		int numLeft;
		int numRight;
		int index;
		Node left;
		Node right;

		public Node( int numLeft, int numRight, Node left, Node right, int index ) {
			this.numLeft = numLeft;
			this.numRight = numRight;
			this.left = left;
			this.right = right;
			this.index = index;
		}

	}

	public static int arr[];

	public static class Tree {
		public Node root;

		public void makeTree() {
			Queue<Node> q = new LinkedList<>();
			q.add(root);
			while (!q.isEmpty()) {
				Node t = q.poll();

				if (t.index * 2 < arr.length) {
					int a = t.index * 4;
					int b = ((t.index * 2) * 2) + 1;
					int a2 = ((t.index * 2) + 1) * 2;
					int b2 = (((t.index * 2) + 1) * 2) + 1;
					if (a < arr.length) {
						t.left = new Node(arr[a - 1], arr[b - 1], null, null,
								t.index * 2);
						t.right = new Node(arr[a2 - 1], arr[b2 - 1], null, null,
								t.index * 2 + 1);
						q.add(t.left);
						q.add(t.right);
					} else {

					}

				}
			}

		}
	}

	public static void main(String[] args) throws IOException {
		long inicio = System.currentTimeMillis();
		StringBuilder out = new StringBuilder();
		BufferedReader in;
		File archivo = new File("entrada");
		if (archivo.exists())
			in = new BufferedReader(new FileReader(archivo));
		else
			in = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(in.readLine());
		arr = readInts(in.readLine());
		int k = (int) Math.pow(2, n);
		long total = 0;
		int m = 0;
		for (int i = arr.length - 1; i >= 0;) {
			int index = i;
			int r = (int) Math.pow(2, n - 1);
			int cont = 0;

			if (i == arr.length - 1)
				while (cont < r) {
					int max = Math.max(arr[index], arr[index - 1]);
					if (arr[index] < arr[index - 1]) {
						total += Math.abs(arr[index] - arr[index - 1]);
						arr[index] += Math.abs(arr[index] - arr[index - 1]);
					} else {
						total += Math.abs(arr[index] - arr[index - 1]);
						arr[index - 1] += Math.abs(arr[index] - arr[index - 1]);
					}
					m = Math.max(m, max);

					index -= 2;
					i -= 2;
					cont++;
				}
			else {
				int m2 = 0;

				while (cont < r) {
					int a = arr[index]
							+ Math.max(arr[(index + 1) * 2], arr[(index + 1) * 2 + 1]);
					int b = arr[index - 1]
							+ Math.max(arr[(index) * 2], arr[(index) * 2 + 1]);
					if (a > b) {
						arr[index-1] += a - b;
						total += a - b;
					} else {
						arr[index] += a - b;
						total += b - a;
					}
					m2 = Math.max(a, b);
					index -= 2;
					i -= 2;
					cont++;
				}
				m = m2;
			}

			n--;
		}
		System.out.println(total);
		System.out.print(out);
		if (archivo.exists())
			System.out.println("Tiempo transcurrido : "
					+ (System.currentTimeMillis() - inicio) + " milisegundos.");
	}
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class Main {

	// split de array
	public static int[] readInts(String cad) {
		String read[] = cad.split(" ");
		int res[] = new int[read.length];
		for (int i = 0; i < read.length; i++) {
			res[i] = Integer.parseInt(read[i]);
		}
		return res;
	}

	public static class Node {
		int numLeft;
		int numRight;
		int index;
		Node left;
		Node right;

		public Node( int numLeft, int numRight, Node left, Node right, int index ) {
			this.numLeft = numLeft;
			this.numRight = numRight;
			this.left = left;
			this.right = right;
			this.index = index;
		}

	}

	public static int arr[];

	public static class Tree {
		public Node root;

		public void makeTree() {
			Queue<Node> q = new LinkedList<>();
			q.add(root);
			while (!q.isEmpty()) {
				Node t = q.poll();

				if (t.index * 2 < arr.length) {
					int a = t.index * 4;
					int b = ((t.index * 2) * 2) + 1;
					int a2 = ((t.index * 2) + 1) * 2;
					int b2 = (((t.index * 2) + 1) * 2) + 1;
					if (a < arr.length) {
						t.left = new Node(arr[a - 1], arr[b - 1], null, null,
								t.index * 2);
						t.right = new Node(arr[a2 - 1], arr[b2 - 1], null, null,
								t.index * 2 + 1);
						q.add(t.left);
						q.add(t.right);
					} else {

					}

				}
			}

		}
	}

	public static void main(String[] args) throws IOException {
		long inicio = System.currentTimeMillis();
		StringBuilder out = new StringBuilder();
		BufferedReader in;
		File archivo = new File("entrada");
		if (archivo.exists())
			in = new BufferedReader(new FileReader(archivo));
		else
			in = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(in.readLine());
		arr = readInts(in.readLine());
		int k = (int) Math.pow(2, n);
		long total = 0;
		int m = 0;
		for (int i = arr.length - 1; i >= 0;) {
			int index = i;
			int r = (int) Math.pow(2, n - 1);
			int cont = 0;

			if (i == arr.length - 1)
				while (cont < r) {
					int max = Math.max(arr[index], arr[index - 1]);
					if (arr[index] < arr[index - 1]) {
						total += Math.abs(arr[index] - arr[index - 1]);
						arr[index] += Math.abs(arr[index] - arr[index - 1]);
					} else {
						total += Math.abs(arr[index] - arr[index - 1]);
						arr[index - 1] += Math.abs(arr[index] - arr[index - 1]);
					}
					m = Math.max(m, max);

					index -= 2;
					i -= 2;
					cont++;
				}
			else {
				int m2 = 0;

				while (cont < r) {
					int a = arr[index]
							+ Math.max(arr[(index + 1) * 2], arr[(index + 1) * 2 + 1]);
					int b = arr[index - 1]
							+ Math.max(arr[(index) * 2], arr[(index) * 2 + 1]);
					if (a > b) {
						arr[index - 1] += a - b;
						total += a - b;
					} else {
						arr[index] += b - a;
						total += b - a;
					}
					m2 = Math.max(a, b);
					index -= 2;
					i -= 2;
					cont++;
				}
				m = m2;
			}

			n--;
		}
		System.out.println(total);
		System.out.print(out);
		if (archivo.exists())
			System.out.println("Tiempo transcurrido : "
					+ (System.currentTimeMillis() - inicio) + " milisegundos.");
	}
}


import java.io.*;
import java.util.*;

public class Main {

    static int adjList [][] , N , ans ;
    public static int pow (int n)
    {
        if (n == 1) return 2;
        return pow(n - 1) << 1;
    }

    public static void dfsCnt (int u)
    {
        if (u >= pow(N)) return ;

        dfsCnt(u << 1);
        dfsCnt(u << 1 | 1);

        ans+= Math.abs(adjList[u][0] - adjList[u][1]);
        if (u != 1)
            adjList[u >> 1][u % 2] +=  Math.max(adjList[u][0] , adjList[u][1]);
    }
    public static void main (String [] args) throws IOException {

        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        StringBuilder sb = new StringBuilder();
        N = sc.nextInt();
        adjList = new int [pow(N + 1) - 1][2];
        for (int i = 1 ; i <= pow(N) - 1 ; ++i)
        {
            adjList[i][0]= sc.nextInt();
            adjList[i][1]= sc.nextInt();
        }
        ans = 0;
        dfsCnt(1);
        System.out.println(ans);
        out.flush();
        out.close();
    }

    static class Scanner
    {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(s));}

        public String next() throws IOException
        {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {return Integer.parseInt(next());}

        public long nextLong() throws IOException {return Long.parseLong(next());}

        public String nextLine() throws IOException {return br.readLine();}

        public double nextDouble() throws IOException
        {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if(x.charAt(0) == '-')
            {
                neg = true;
                start++;
            }
            for(int i = start; i < x.length(); i++)
                if(x.charAt(i) == '.')
                {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                }
                else
                {
                    sb.append(x.charAt(i));
                    if(dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg?-1:1);
        }

        public boolean ready() throws IOException {return br.ready();}

    }
}


import java.io.*;
import java.util.*;

public class Main {

    static int adjList [][] , N , max , cnt , add1 , add2;
    public static int pow (int n)
    {
        if (n == 1) return 2;
        return pow(n - 1) << 1;
    }
    public static int dfsMax (int u)
    {
        if (u >= pow(N)) return 0;
        return Math.max(adjList[u][0] + dfsMax(u << 1) , adjList[u][1] + dfsMax(u << 1 | 1));
    }

    public static void dfsCnt (int u  , int sumSoFar)
    {
        if (u >= (pow(N) + pow(N + 1)) >> 1)
        {
            int diff = Math.max(0 , max - (sumSoFar + add2));
            cnt += diff;
            add2 += diff;
        }
        else if (u >= pow(N))
        {
            int diff = Math.max(0 , max - (sumSoFar + add1));
            cnt += diff;
            add1 += diff;
        }
        else
        {
            dfsCnt(u << 1 , sumSoFar + adjList[u][0]);
            dfsCnt(u << 1 | 1 , sumSoFar + adjList[u][1]);
        }
    }
    public static void main (String [] args) throws IOException {

        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        StringBuilder sb = new StringBuilder();
        N = sc.nextInt();
        adjList = new int [pow(N + 1) - 1][2];
        for (int i = 1 ; i <= pow(N) - 1 ; ++i)
        {
            adjList[i][0]= sc.nextInt();
            adjList[i][1]= sc.nextInt();
        }

        max = dfsMax (1);
        cnt = 0;
        add1 = 0;
        add2 = 0;
        dfsCnt(1,0);
        System.out.println(cnt);
        out.flush();
        out.close();
    }

    static class Scanner
    {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(s));}

        public String next() throws IOException
        {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {return Integer.parseInt(next());}

        public long nextLong() throws IOException {return Long.parseLong(next());}

        public String nextLine() throws IOException {return br.readLine();}

        public double nextDouble() throws IOException
        {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if(x.charAt(0) == '-')
            {
                neg = true;
                start++;
            }
            for(int i = start; i < x.length(); i++)
                if(x.charAt(i) == '.')
                {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                }
                else
                {
                    sb.append(x.charAt(i));
                    if(dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg?-1:1);
        }

        public boolean ready() throws IOException {return br.ready();}

    }
}


import java.awt.Container;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.LinkedBlockingDeque;

import javax.swing.border.Border;

public class a {

	public static long mod = (long) Math.pow(10, 9) + 7;

	private static class node implements Comparable<node> {
		int to;
		int R;
		int L;

		node(int a, int b, int c) {
			to = a;
			R = b;
			L = c;

		}

		@Override
		public int compareTo(node o) {
			if (o.R < R)
				return 1;
			else if (o.R > R)
				return -1;
			else if (o.L < L)
				return 1;
			else if (o.L > L)
				return -1;
			return 0;
		}

	}

	public static class point {
		int screen;
		int order;

		point(int x, int y) {
			screen = x;
			order = y;

		}

	}

	public static int ch(long y) {
		int r = Long.bitCount(y);
		return r;
	}

	public static long gcd(long x, long y) {
		if (y == 0)
			return x;

		return gcd(y, x % y);
	}

	public static boolean ch[] = new boolean[10000005];
	public static ArrayList<Integer> prime = new ArrayList();

	// public static Queue<Integer> pp;

	public static void sieve(int k) {
		ch[0] = ch[1] = true;

		for (int i = 2; i <= k; i++) {
			if (!ch[i]) {
				prime.add(i);
				// pp.add(i);
				for (long j = (long) i * (long) i; j <= k; j += i) {
					ch[(int) j] = true;
				}
			}
		}

	}

	public static long pw(int x, int power, int mod) {
		if (power == 1)
			return x % mod;
		else if (power % 2 == 0)
			return pw((x % mod * x % mod) % mod, power / 2, mod);
		else
			return (pw(x, power - 1, mod) % mod * x % mod) % mod;

	}

	public static String addToString(String a, int index, char b) {
		String ans = "";
		for (int i = 0; i < a.length(); i++) {
			if (i == index) {
				ans += b;
				index = -1;
				i--;
				continue;
			}
			ans += a.charAt(i);
		}
		return ans;
	}

	public static int bit[];

	public static void add(int idx, int cost) {
		while (idx < bit.length) {
			bit[idx] += cost;
			idx += (idx & -idx);
		}

	}

	public static int get(int idx) {
		int res = 0;
		while (idx > 0) {
			res += bit[idx];
			idx -= (idx & -idx);
		}
		return res;
	}

	public static int dx[] = { 0, 1, 0, -1 };
	public static int dy[] = { 1, 0, -1, 0 };
	public static long arr[];
	public static int n;
	public static int limit;
	public static long max;

	public static long solve(int p) {
		if (p >= limit)
			return 0;
		long a = solve(p * 2) + arr[p * 2];
		long b = solve(p * 2 + 1) + arr[p * 2 + 1];

		return Math.max(a, b);
	}

	public static long ans = 0;

	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		// BufferedReader in = new BufferedReader(new FileReader(
		// "corporate_gifting.txt"));
		StringBuilder qq = new StringBuilder();
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		// PrintWriter out = new PrintWriter(new FileWriter("out.txt"));
		// System.out.println('a' - 'A');
		n = Integer.parseInt(in.readLine());
		arr = new long[1 << (n + 2)];
		limit = 1 << (n + 1);
		String y[] = in.readLine().split(" ");
		for (int i = 1; i <= y.length; i++) {
			arr[i + 1] = Long.parseLong(y[i - 1]);
		}
		max = solve(1);
		long ans = 0;
		arr[1] = max;
		long dp[] = new long[1 << (n + 2)];
		for (int i = n; i >= 1; i--) {
			for (int j = 1 << i; j < 1 << (i + 1); j += 2) {
				if (i == n) {
					ans += Math.abs(arr[j] - arr[j - 1]);
					dp[j / 2] = Math.max(arr[j], arr[j - 1]);
				} else {
					long p = arr[j] + dp[j];
					long p2 = arr[j + 1] + dp[j + 1];
					ans += Math.abs(p - p2);
					dp[j / 2] = Math.max(p, p2);
				}

			}
		}
		System.out.println(ans);

		out.close();

	}
}


import java.awt.Container;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.LinkedBlockingDeque;

import javax.swing.border.Border;

public class a {

	public static long mod = (long) Math.pow(10, 9) + 7;

	private static class node implements Comparable<node> {
		int to;
		int R;
		int L;

		node(int a, int b, int c) {
			to = a;
			R = b;
			L = c;

		}

		@Override
		public int compareTo(node o) {
			if (o.R < R)
				return 1;
			else if (o.R > R)
				return -1;
			else if (o.L < L)
				return 1;
			else if (o.L > L)
				return -1;
			return 0;
		}

	}

	public static class point {
		int screen;
		int order;

		point(int x, int y) {
			screen = x;
			order = y;

		}

	}

	public static int ch(long y) {
		int r = Long.bitCount(y);
		return r;
	}

	public static long gcd(long x, long y) {
		if (y == 0)
			return x;

		return gcd(y, x % y);
	}

	public static boolean ch[] = new boolean[10000005];
	public static ArrayList<Integer> prime = new ArrayList();

	// public static Queue<Integer> pp;

	public static void sieve(int k) {
		ch[0] = ch[1] = true;

		for (int i = 2; i <= k; i++) {
			if (!ch[i]) {
				prime.add(i);
				// pp.add(i);
				for (long j = (long) i * (long) i; j <= k; j += i) {
					ch[(int) j] = true;
				}
			}
		}

	}

	public static long pw(int x, int power, int mod) {
		if (power == 1)
			return x % mod;
		else if (power % 2 == 0)
			return pw((x % mod * x % mod) % mod, power / 2, mod);
		else
			return (pw(x, power - 1, mod) % mod * x % mod) % mod;

	}

	public static String addToString(String a, int index, char b) {
		String ans = "";
		for (int i = 0; i < a.length(); i++) {
			if (i == index) {
				ans += b;
				index = -1;
				i--;
				continue;
			}
			ans += a.charAt(i);
		}
		return ans;
	}

	public static int bit[];

	public static void add(int idx, int cost) {
		while (idx < bit.length) {
			bit[idx] += cost;
			idx += (idx & -idx);
		}

	}

	public static int get(int idx) {
		int res = 0;
		while (idx > 0) {
			res += bit[idx];
			idx -= (idx & -idx);
		}
		return res;
	}

	public static int dx[] = { 0, 1, 0, -1 };
	public static int dy[] = { 1, 0, -1, 0 };
	public static long arr[];
	public static int n;
	public static int limit;
	public static long max;

	public static long solve(int p) {
		if (p >= limit)
			return 0;
		long a = solve(p * 2) + arr[p * 2];
		long b = solve(p * 2 + 1) + arr[p * 2 + 1];

		return Math.max(a, b);
	}

	public static long ans = 0;

	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		// BufferedReader in = new BufferedReader(new FileReader(
		// "corporate_gifting.txt"));
		StringBuilder qq = new StringBuilder();
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		// PrintWriter out = new PrintWriter(new FileWriter("out.txt"));
		// System.out.println('a' - 'A');
		n = Integer.parseInt(in.readLine());
		arr = new long[1 << (n + 2)];
		limit = 1 << (n + 1);
		String y[] = in.readLine().split(" ");
		for (int i = 1; i <= y.length; i++) {
			arr[i + 1] = Long.parseLong(y[i - 1]);
		}
		max = solve(1);
		long ans = 0;
		arr[1] = max;
		long dp[] = new long[1 << (n + 2)];
		for (int i = n; i >= 1; i--) {
			for (int j = 1 << i; j < 1 << (i + 1); j += 2) {
				//System.out.print(j + " ");
				if (i == n) {
					ans += Math.abs(arr[j] - arr[j + 1]);
					dp[j / 2] = Math.max(arr[j], arr[j + 1]);
				} else {
					long p = arr[j] + dp[j];
					long p2 = arr[j + 1] + dp[j + 1];
					ans += Math.abs(p - p2);
					dp[j / 2] = Math.max(p, p2);
				}

			}
			System.out.println();
		}
		System.out.println(ans);

		out.close();

	}
}


import java.util.*;

public class Main {

	public static void main(String[] args) {

		Scanner scn = new Scanner(System.in);

		int n = scn.nextInt();

		int t = (int) (Math.pow(2, n + 1)) - 1;

		int arr[] = new int[t + 1];

		for (int i = 2; i <= t; i++) {
			arr[i] = scn.nextInt();
		}

		int ans = 0;

		for (int i = (int) Math.pow(2, n) - 1; i > 0; i--) {

			arr[i] += Math.max(arr[2 * i], arr[2 * i + 1]);

			ans += Math.abs(arr[2 * i] - arr[2 * i + 1]);
		}

		System.out.println(ans);

	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class C526B {
	private StringTokenizer st;
	private BufferedReader bf;

	public class MyTree {
	   private Node root;     // root of the BST
	    private int N;         // number of key-value pairs in BST

	    // BST helper node data type
	    private class Node {
	        private Integer key;           // key
	        private Integer val;         // associated data
	        private Integer light;         // associated data
	        private Node left, right;  // links to left and right subtrees

	        public Node(Integer key, Integer val) {
	            this.key = key;
	            this.val = val;
	            this.light = val;
	        }
	    }

	    public Integer get(int key) {
	    	return get(root, key);
	    }

	    private Integer get(Node x, Integer key) {
	    	while (x != null) {
		    	int cmp = key.compareTo(x.key);
		    	if      (cmp < 0) x = x.left;
		    	else if (cmp > 0) x = x.right;
		    	else              return x.val;
	    	}
	   		return null;
	    }

	    public void put(Integer key, Integer val) {
	    	root = put(root, key, val);
	    }

	    public Node put(Node x, Integer key, Integer val) {
	    	if (x == null)
	    		return new Node(key, val);
	    	int cmp = key.compareTo(x.key);
	    	if      (cmp < 0) x.left = put(x.left, key, val);
	    	else if (cmp > 0) x.right = put(x.right, key, val);
	    	if (x.left != null && x.right != null) {
	    		x.light = x.val + Math.max(x.left.light, x.left.light);
	    	}
	    	return x;
	    }

	    public Integer walk() {

	    	return 0;
	    }

	}

	public C526B(){
		try {
			bf = new BufferedReader(new InputStreamReader(System.in));
			st = new StringTokenizer(bf.readLine());
			MyTree mt = new MyTree();
			int n = nextInt();
			int square = 1;
			for (int i = 1; i <= n; i++) {
				square = square + (int)Math.pow(2, i);
			}
			int[] a = new int[square+1];
			int result = 0;
			int m = (int)Math.pow(2, n+1)-1;
			int index = 2;
			for (int i = 2; i <= m; i++) {
				int lights = nextInt();
				a[index++] = lights;
			}
			int pos = (int)Math.pow(2, n);
			int path = walk(a,1,m);
			result = setwalk(a,1,m,0,path);
			System.out.println(result);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private int walk(int[] a, int i, int m) {
		int l = i*2;
		int r = i*2+1;
		if (l > m || r > m) {
			return a[i];
		}
		int b = walk(a, l,m);
		int c = walk(a, r,m);
		return (a[i]+Math.max(b, c));
	}

	private int setwalk(int[] a, int i, int m, int v, int goal) {
		int l = i*2;
		int r = i*2+1;
		if (l > m || r > m) {
			return goal-(a[i]+v);
		}
		int b = setwalk(a, l,m, v+a[i], goal);
		int c = setwalk(a, r,m, v+a[i], goal);
		return (b+c);
	}

	private int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	private String next() throws IOException {
		if (st == null || !st.hasMoreTokens())
			st = new StringTokenizer(bf.readLine());
		return st.nextToken();
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		C526B c = new C526B();
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class C526B {
	private StringTokenizer st;
	private BufferedReader bf;

	public C526B(){
		try {
			bf = new BufferedReader(new InputStreamReader(System.in));
			st = new StringTokenizer(bf.readLine());

			int n = nextInt();
			int square = 1;
			for (int i = 1; i <= n; i++) {
				square = square + (int)Math.pow(2, i);
			}
			int[] a = new int[square+1];
			int[] mx = new int[square+1];
			int result = 0;
			int m = (int)Math.pow(2, n+1)-1;
			int index = 2;
			for (int i = 2; i <= m; i++) {
				int lights = nextInt();
				a[index++] = lights;
			}
			int pos = (int)Math.pow(2, n);
			int path = walk(a,1,m, mx);
			result = setwalk(a,mx,1,m,path);
			System.out.println(result);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private int walk(int[] a, int i, int m, int[] mx) {
		int l = i*2;
		int r = i*2+1;
		if (l > m || r > m) {
			return a[i];
		}
		int b = walk(a, l,m, mx);
		int c = walk(a, r,m, mx);
		mx[i] = Math.max(b, c);
		return (a[i]+Math.max(b, c));
	}

	private int setwalk(int[] a, int[] mx, int i, int m, int maxval) {
		int result = 0;
		int l = i*2;
		int r = i*2+1;
		if (l > m || r > m) {
			return 0;
		}

		int sub = a[l];
		if ((a[l] + mx[l]) < maxval) {
			int diff = maxval - (a[l] + mx[l]);
			sub += diff;
			result += diff;
		}
		int b = setwalk(a, mx, l,m, maxval - sub);

		sub = a[r];
		if ((a[r] + mx[r]) < maxval) {
			int diff = maxval - (a[r] + mx[r]);
			sub += diff;
			result += diff;
		}
		int c = setwalk(a, mx, r,m, maxval - sub);
		return (result+b+c);
	}

	private int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	private String next() throws IOException {
		if (st == null || !st.hasMoreTokens())
			st = new StringTokenizer(bf.readLine());
		return st.nextToken();
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		C526B c = new C526B();
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        static int _n;
        static int res = 0;
        static int[] light;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            _n = (1 << (n + 1)) - 2;
            light = new int[_n];
            for (int i = 0; i < _n; i++) {
                light[i] = in.nextInt();
            }
            dfs(1);
            out.println(res);
        }

        static int dfs(int node) {
            int l = 2 * node;
            int r = 2 * node + 1;
            if (l <= _n) {
                int LLight = dfs(l);
                int RLight = dfs(r);
                int maxL = Math.max(LLight, RLight);
                res += Math.abs(LLight - RLight);
                if (node <= 1) return maxL;
                return light[node - 2] + maxL;
            }
            return light[node - 2];
        }

    }

    static class InputReader {
        private StringTokenizer tokenizer;
        private BufferedReader reader;

        public InputReader(InputStream inputStream) {
            reader = new BufferedReader(new InputStreamReader(inputStream));
        }

        private void fillTokenizer() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }

        public String next() {
            fillTokenizer();
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;

public class B {
	static int[] array;
	public static void main(String[] args) throws Exception {
		FinalScanner sc = new FinalScanner();
		PrintWriter out = new PrintWriter(System.out);
		int N = sc.nextInt();
		int S = 2<<N;
		array = new int[S];
		for(int a=0;a<S-2;a++){
			array[a+2]=sc.nextInt();
		}
		out.println(magic(1,N)[0]);
	//	System.out.println(Arrays.toString(array));
		out.close();
	}

	private static long[] magic(int pos, int N) {
		if(N==0)return new long[]{0,0};
		long[] left = magic(pos*2,N-1);
		left[1]+=array[pos*2];
		long[] right = magic(pos*2 +1,N-1);
		right[1]+=array[pos*2 +1];
		long size = Math.max(left[1],right[1]);
		long cost = left[0]+right[0] + size+size-left[1]-right[1];
		return new long[] {cost,size};
	}

	static class FinalScanner{
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public FinalScanner(){
			stream = System.in;
			//stream = new FileInputStream(new File("dec.in"));

		}
		int read(){
			if(numChars==-1)
				throw new InputMismatchException();
			if(curChar>=numChars){
				curChar = 0;
				try{
					numChars = stream.read(buf);
				} catch (IOException e){
					throw new InputMismatchException();
				}
				if(numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		boolean isSpaceChar(int c){
			return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
		}
		boolean isEndline(int c){
			return c=='\n'||c=='\r'||c==-1;
		}
		int nextInt(){
			return Integer.parseInt(next());
		}
		int[] nextInt(int v){
			int[] temp = new int[v];
			for(int a=0;a<v;a++)temp[a]=nextInt();
			return temp;
		}
		long nextLong(){
			return Long.parseLong(next());
		}
		long[] nextLong(int v){
			long[] temp = new long[v];
			for(int a=0;a<v;a++)temp[a]=nextLong();
			return temp;
		}
		double nextDouble(){
			return Double.parseDouble(next());
		}
		double[] nextDouble(int v){
			double[] temp = new double[v];
			for(int a=0;a<v;a++)temp[a]=nextDouble();
			return temp;
		}
		String next(){
			int c = read();
			while(isSpaceChar(c))
				c=read();
			StringBuilder res = new StringBuilder();
			do{
				res.appendCodePoint(c);
				c=read();
			} while(!isSpaceChar(c));
			return res.toString();
		}
		String[] next(int v){
			String[] temp = new String[v];
			for(int a=0;a<v;a++)temp[a]=next();
			return temp;
		}
		String nextLine(){
			int c = read();
			while(isEndline(c))
				c=read();
			StringBuilder res = new StringBuilder();
			do{
				res.appendCodePoint(c);
				c = read();
			}while(!isEndline(c));
			return res.toString();
		}
		String[] nextLine(int v){
			String[] temp = new String[v];
			for(int a=0;a<v;a++)temp[a]=nextLine();
			return temp;
		}

	}
}


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;

public class B {
	static int[] array;
	public static void main(String[] args) throws Exception {
		FinalScanner sc = new FinalScanner();
		PrintWriter out = new PrintWriter(System.out);
		int N = sc.nextInt();
		int S = 2<<N;
		array = new int[S];
		for(int a=0;a<S-2;a++){
			array[a+2]=sc.nextInt();
		}
		out.println(magic(1,N)[1]);
	//	System.out.println(Arrays.toString(array));
		out.close();
	}

	private static long[] magic(int pos, int N) {
		if(N==0)return new long[]{0,0};
		long[] left = magic(pos*2,N-1);
		left[1]+=array[pos*2];
		long[] right = magic(pos*2 +1,N-1);
		right[1]+=array[pos*2 +1];
		long size = Math.max(left[1],right[1]);
		long cost = left[0]+right[0] + size+size-left[1]-right[1];
		return new long[] {cost,size};
	}

	static class FinalScanner{
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public FinalScanner(){
			stream = System.in;
			//stream = new FileInputStream(new File("dec.in"));

		}
		int read(){
			if(numChars==-1)
				throw new InputMismatchException();
			if(curChar>=numChars){
				curChar = 0;
				try{
					numChars = stream.read(buf);
				} catch (IOException e){
					throw new InputMismatchException();
				}
				if(numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		boolean isSpaceChar(int c){
			return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
		}
		boolean isEndline(int c){
			return c=='\n'||c=='\r'||c==-1;
		}
		int nextInt(){
			return Integer.parseInt(next());
		}
		int[] nextInt(int v){
			int[] temp = new int[v];
			for(int a=0;a<v;a++)temp[a]=nextInt();
			return temp;
		}
		long nextLong(){
			return Long.parseLong(next());
		}
		long[] nextLong(int v){
			long[] temp = new long[v];
			for(int a=0;a<v;a++)temp[a]=nextLong();
			return temp;
		}
		double nextDouble(){
			return Double.parseDouble(next());
		}
		double[] nextDouble(int v){
			double[] temp = new double[v];
			for(int a=0;a<v;a++)temp[a]=nextDouble();
			return temp;
		}
		String next(){
			int c = read();
			while(isSpaceChar(c))
				c=read();
			StringBuilder res = new StringBuilder();
			do{
				res.appendCodePoint(c);
				c=read();
			} while(!isSpaceChar(c));
			return res.toString();
		}
		String[] next(int v){
			String[] temp = new String[v];
			for(int a=0;a<v;a++)temp[a]=next();
			return temp;
		}
		String nextLine(){
			int c = read();
			while(isEndline(c))
				c=read();
			StringBuilder res = new StringBuilder();
			do{
				res.appendCodePoint(c);
				c = read();
			}while(!isEndline(c));
			return res.toString();
		}
		String[] nextLine(int v){
			String[] temp = new String[v];
			for(int a=0;a<v;a++)temp[a]=nextLine();
			return temp;
		}

	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class OmNomDarkPark {
	static IR in = new IR(System.in);
	static Node tree[] = new Node[1<<15];
	static int N;
	static int NODES;

	static class Node{
		int edge;
		int cost;
		int c1;
		int c2;
		public Node(int edge,int child1,int child2){
			this.edge = edge;
			this.c1= child1;
			this.c2=child2;
		}
	}

	public static void main(String[]args)throws Throwable{
		N = in.j();
		int read = (1<<(N+1))-2;
		int idx = 2;
		int cost = 0;
		tree[1] = new Node(0,-1,-1);
		while(read-->0){
			cost = in.j();
			tree[idx] = new Node(cost,-1,-1);
			idx++;

		}

		NODES = (1<<(N+1))-1;
		long soln = 0;
		for(int i=NODES;i>0;i--){
			int diff = 0;
			int dist = 0;
			int edge = tree[i].edge;
			if(tree[i].c1!=-1&&tree[i].c2!=-1){
				diff = Math.abs(tree[i].c1-tree[i].c2);
				dist = Math.max(tree[i].c1,tree[i].c2);
			}
			soln += diff;
			int parent = i/2;
			if(i==1) break;
			if(tree[parent].c1==-1) tree[parent].c1 = edge+dist;
			else tree[parent].c2 = edge+dist;

		}

		System.out.println(soln);

	}

	static	class IR {
		public BufferedReader reader;
		public StringTokenizer tokenizer;

		public IR(InputStream stream) {
			reader = new BufferedReader(new InputStreamReader(stream), 32768);
			tokenizer = null;
		}

		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}

		public int j() {
			return Integer.parseInt(next());
		}
		public long ll(){
			return Long.parseLong(next());
		}

	}
}


import java.util.*;
import java.io.*;

public class Solution{

	BufferedReader br;
	long res = 0;

	class Node{
		int v;
		int weight;
		Node left;
		Node right;

		private Node(int w){
			this.weight = w;
		}

	}

	public void findDiff(Node root){

		if(root == null){
			return ;
		}

		int left = 0;
		int right = 0;
		if(root.left !=null){
			findDiff(root.left);
			left = root.left.weight;
		}
		if(root.right !=null){
			findDiff(root.right);
			right = root.right.weight;
		}
		res += Math.abs(left - right);

		if(left > right)
			root.right.weight += left - right;
		else if(right>0)
			root.left.weight += right - left;

	}

	public long findDiff2(Node root){

		if(root == null){
			return 0;
		}

		long left = findDiff2(root.left);
		long right = findDiff2(root.right);

		return (root.weight + left + right);

	}

	public void run() throws IOException{
		br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine().trim());

		int size = (int)Math.pow(2,n+1)-1;

		Node root = new Node(0);

		Node [] arr = new Node[size];
		arr[0] = root;

		String [] strArr = br.readLine().trim().split(" ");
		int start = 0;
		boolean left = true;

		int index = 2;
		for(int i=1;i<size;i++){
			int w = Integer.parseInt(strArr[start++]);
			int parent = index++/2;
			parent--;
			if(left){
				arr[parent].left = new Node(w);
				arr[i] = arr[parent].left;
			}
			else{
				arr[parent].right = new Node(w);
				arr[i] = arr[parent].right;
			}

			left = !left;

		}

		findDiff(root);
		long prev = findDiff2(root.left);
		long after = findDiff2(root.right);
		res += (after-prev)/Math.pow(2, n-1);

		System.out.println(res);
	}

	public static void main(String [] args)throws IOException{
		new Solution().run();
	}
}


import java.util.*;
import java.io.*;

public class Solution{

	BufferedReader br;
	long max = 0;

	class Node{
		int v;
		int weight;
		Node left;
		Node right;

		private Node(int w){
			this.weight = w;
		}

	}

	public long findDiff(Node root,long sum){

		if(root == null){
			max = Math.max(max,sum);
			return 0;
		}

		long left = findDiff(root.left,sum+root.weight);
		long right = findDiff(root.right,sum+root.weight);

		return (root.weight + left + right);

	}

	public void run() throws IOException{
		br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine().trim());

		int size = (int)Math.pow(2,n+1)-1;

		Node root = new Node(0);

		Node [] arr = new Node[size];
		arr[0] = root;

		String [] strArr = br.readLine().trim().split(" ");
		int start = 0;
		boolean left = true;

		int index = 2;
		for(int i=1;i<size;i++){
			int w = Integer.parseInt(strArr[start++]);
			int parent = index++/2;
			parent--;
			if(left){
				arr[parent].left = new Node(w);
				arr[i] = arr[parent].left;
			}
			else{
				arr[parent].right = new Node(w);
				arr[i] = arr[parent].right;
			}

			left = !left;

		}

		long sum = findDiff(root,0);

		System.out.println(Math.abs(max*Math.pow(2,n)-sum));
	}

	public static void main(String [] args)throws IOException{
		new Solution().run();
	}
}


import java.util.*;
import java.io.*;

public class Solution{

	BufferedReader br;
	long max = 0;
	long res = 0;

	class Node{
		int v;
		int weight;
		Node left;
		Node right;

		private Node(int w){
			this.weight = w;
		}

	}

	public long findDiff(Node root,long sum){

		if(root == null){
			max = Math.max(max,sum);
			return 0;
		}

		long left = findDiff(root.left,sum+root.weight);
		long right = findDiff(root.right,sum+root.weight);

		return (root.weight + left + right);

	}

	public long findDiff2(Node root,long sum){

		if(root == null){
			res += max - sum;
			return 0;
		}

		long left = findDiff(root.left,sum+root.weight);
		long right = findDiff(root.right,sum+root.weight);

		return (root.weight + left + right);

	}

	public void run() throws IOException{
		br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine().trim());

		int size = (int)Math.pow(2,n+1)-1;

		Node root = new Node(0);

		Node [] arr = new Node[size];
		arr[0] = root;

		String [] strArr = br.readLine().trim().split(" ");
		int start = 0;
		boolean left = true;

		int index = 2;
		for(int i=1;i<size;i++){
			int w = Integer.parseInt(strArr[start++]);
			int parent = index++/2;
			parent--;
			if(left){
				arr[parent].left = new Node(w);
				arr[i] = arr[parent].left;
			}
			else{
				arr[parent].right = new Node(w);
				arr[i] = arr[parent].right;
			}

			left = !left;

		}

		findDiff(root,0);
		findDiff2(root,0);

		System.out.println(res);
	}

	public static void main(String [] args)throws IOException{
		new Solution().run();
	}
}


import java.util.*;
import java.io.*;

public class Solution{

	BufferedReader br;

	class Node{
		int v;
		int weight;
		Node left;
		Node right;

		private Node(int w){
			this.weight = w;
		}

	}

	public long findDiff(Node root){

		if(root == null)
			return 0;

		long left = findDiff(root.left);
		long right = findDiff(root.right);

		return (root.weight + left + right);

	}

	public void run() throws IOException{
		br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine().trim());

		int size = Math.pow(2,n+1)-1;

		Node root = new Node();

		Node [] arr = new Node[size];
		arr[0] = root;

		String [] strArr = br.readLine().trim().split();
		int start = 0;
		boolean left = true;

		int index = 2;
		for(int i=1;i<size;i++){
			int w = Integer.parseInt(strArr[start++]);
			int parent = index++/2;
			parent--;
			if(left){
				arr[parent].left = new Node(w);
				arr[i] = arr[parent].left;
			}
			else{
				arr[parent].right = new Node(w);
				arr[i] = arr[parent].right;
			}

			left = !left;

		}

		long left = findDiff(root.left);
		long right = findDiff(root.right);

		System.out.println(Math.abs(left-right));
	}

	public static void main(String [] args)throws IOException{
		new Solution().run();
	}
}


import java.util.*;
import java.io.*;

public class Solution{

	BufferedReader br;
	long res = 0;

	class Node{
		int v;
		int weight;
		Node left;
		Node right;

		private Node(int w){
			this.weight = w;
		}

	}

	public void findDiff(Node root){

		if(root.left == null)
			return;

		findDiff(root.left);
		findDiff(root.right);

		root.weight += Math.max(root.left.weight, root.right.weight);

		res += Math.abs(root.left.weight - root.right.weight);

	}

	public void run() throws IOException{
		br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine().trim());

		int size = (int)Math.pow(2,n+1)-1;

		Node root = new Node(0);

		Node [] arr = new Node[size];
		arr[0] = root;

		String [] strArr = br.readLine().trim().split(" ");
		int start = 0;
		boolean left = true;

		int index = 2;
		for(int i=1;i<size;i++){
			int w = Integer.parseInt(strArr[start++]);
			int parent = index++/2;
			parent--;
			if(left){
				arr[parent].left = new Node(w);
				arr[i] = arr[parent].left;
			}
			else{
				arr[parent].right = new Node(w);
				arr[i] = arr[parent].right;
			}

			left = !left;

		}

		findDiff(root);
		System.out.println(res);
	}

	public static void main(String [] args)throws IOException{
		new Solution().run();
	}
}


import java.util.*;
import java.io.*;

public class Solution{

	BufferedReader br;
	long res = 0;

	class Node{
		int v;
		int weight;
		Node left;
		Node right;

		private Node(int w){
			this.weight = w;
		}

	}

	public long findDiff(Node root){

		if(root == null){
			return 0;
		}

		int left = 0;
		int right = 0;
		if(root.left !=null){
			findDiff(root.left);
			left = root.left.weight;
		}
		if(root.right !=null){
			findDiff(root.right);
			right = root.right.weight;
		}
		//res += Math.abs(left - right);

		if(left > right){
			root.right.weight += left - right;
		}
		else if(right>0)
			root.left.weight += right - left;

		if(root.left != null)
			root.weight += root.left.weight;

		if(root.right != null)
			root.weight += root.right.weight;

		return root.weight;

	}

	public long findDiff2(Node root){

		if(root == null){
			return 0;
		}

		long left = findDiff2(root.left);
		long right = findDiff2(root.right);

		return (root.weight + left + right);

	}

	public void run() throws IOException{
		br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine().trim());

		int size = (int)Math.pow(2,n+1)-1;

		Node root = new Node(0);

		Node [] arr = new Node[size];
		arr[0] = root;

		String [] strArr = br.readLine().trim().split(" ");
		int start = 0;
		boolean left = true;

		int index = 2;
		for(int i=1;i<size;i++){
			int w = Integer.parseInt(strArr[start++]);
			int parent = index++/2;
			parent--;
			if(left){
				arr[parent].left = new Node(w);
				arr[i] = arr[parent].left;
			}
			else{
				arr[parent].right = new Node(w);
				arr[i] = arr[parent].right;
			}

			left = !left;

		}

		System.out.println(Math.abs(findDiff(root.left)- findDiff(root.right)));
	}

	public static void main(String [] args)throws IOException{
		new Solution().run();
	}
}


import java.util.*;
import java.io.*;

public class Solution{

	BufferedReader br;
	long max = 0;

	class Node{
		int v;
		int weight;
		Node left;
		Node right;

		private Node(int w){
			this.weight = w;
		}

	}

	public long findDiff(Node root,long sum){

		if(root == null){
			max = Math.max(max,sum);
			return 0;
		}

		long left = findDiff(root.left,sum+root.weight);
		long right = findDiff(root.right,sum+root.weight);

		return (root.weight + left + right);

	}

	public void run() throws IOException{
		br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine().trim());

		int size = (int)Math.pow(2,n+1)-1;

		Node root = new Node(0);

		Node [] arr = new Node[size];
		arr[0] = root;

		String [] strArr = br.readLine().trim().split(" ");
		int start = 0;
		boolean left = true;

		int index = 2;
		for(int i=1;i<size;i++){
			int w = Integer.parseInt(strArr[start++]);
			int parent = index++/2;
			parent--;
			if(left){
				arr[parent].left = new Node(w);
				arr[i] = arr[parent].left;
			}
			else{
				arr[parent].right = new Node(w);
				arr[i] = arr[parent].right;
			}

			left = !left;

		}

		long sum = findDiff(root,0);

		System.out.println(Math.abs(max*pow(2,n)-sum));
	}

	public static void main(String [] args)throws IOException{
		new Solution().run();
	}
}


import java.util.*;
import java.io.*;

public class Solution{

	BufferedReader br;

	class Node{
		int v;
		int weight;
		Node left;
		Node right;

		private Node(int w){
			this.weight = w;
		}

	}

	public long findDiff(Node root){

		if(root == null)
			return 0;

		long left = findDiff(root.left);
		long right = findDiff(root.right);

		return (root.weight + left + right);

	}

	public void run() throws IOException{
		br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine().trim());

		int size = (int)Math.pow(2,n+1)-1;

		Node root = new Node(0);

		Node [] arr = new Node[size];
		arr[0] = root;

		String [] strArr = br.readLine().trim().split(" ");
		int start = 0;
		boolean left = true;

		int index = 2;
		for(int i=1;i<size;i++){
			int w = Integer.parseInt(strArr[start++]);
			int parent = index++/2;
			parent--;
			if(left){
				arr[parent].left = new Node(w);
				arr[i] = arr[parent].left;
			}
			else{
				arr[parent].right = new Node(w);
				arr[i] = arr[parent].right;
			}

			left = !left;

		}

		long l = findDiff(root.left);
		long right = findDiff(root.right);

		System.out.println(Math.abs(l-right));
	}

	public static void main(String [] args)throws IOException{
		new Solution().run();
	}
}