#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double EPS = 1e-7;
struct Node {
  int id;
  int val;
  int sum;
  int parent() { return (id - 1) / 2; }
  int left() { return id * 2 + 1; }
  int right() { return id * 2 + 2; }
};
Node nodes[2100];
int N, M, nNodes;
void dfs(int id) {
  if (id >= nNodes) return;
  Node &currNode = nodes[id];
  if (id != 0) {
    currNode.sum += nodes[currNode.parent()].sum;
  }
  currNode.sum += currNode.val;
  dfs(currNode.left());
  dfs(currNode.right());
}
pair<int, int> dfs2(int id, const int &maxx) {
  pair<int, int> ret;
  if (nodes[id].left() >= nNodes) {
    ret = make_pair(maxx - nodes[id].sum, 0);
  } else {
    pair<int, int> retLeft, retRight;
    retLeft = dfs2(nodes[id].left(), maxx);
    retRight = dfs2(nodes[id].right(), maxx);
    if (id == 0) {
      ret = make_pair(
          0, retLeft.first + retRight.first + retLeft.second + retRight.second);
    } else {
      int minnNeed = min(retLeft.first, retRight.first);
      int maxxNeed = max(retLeft.first, retRight.first);
      ret = make_pair(minnNeed,
                      retLeft.second + retRight.second + (maxxNeed - minnNeed));
    }
  }
  return ret;
}
int main() {
  scanf("%d", &N);
  nNodes = (1 << (N + 1)) - 1;
  M = nNodes - 1;
  for (int(i) = 0, (_batas) = (nNodes); (i) < (_batas); (i)++) {
    nodes[i].id = i;
    nodes[i].sum = 0;
  }
  for (int(i) = 0, (_batas) = (M); (i) < (_batas); (i)++)
    scanf("%d", &nodes[i + 1].val);
  dfs(0);
  int maxx = 0;
  for (int(i) = 0, (_batas) = (nNodes); (i) < (_batas); (i)++)
    maxx = max(maxx, nodes[i].sum);
  pair<int, int> ans = dfs2(0, maxx);
  printf("%d\n", ans.second);
  return 0;
}
