public static void main ( String [ ] args ) {
  int n = in . nextInt ( ) ;
  int [ ] a = new int [ ( int ) Math . pow ( 2 , n + 1 ) ] ;
  int [ ] c = new int [ ( int ) Math . pow ( 2 , n + 1 ) ] ;
  for ( int i = 2 ;
  i < Math . pow ( 2 , n + 1 ) ;
  i ++ ) {
    a [ i ] = in . nextInt ( ) ;
  }
  for ( int i = 2 ;
  i < Math . pow ( 2 , n + 1 ) ;
  i ++ ) {
    c [ i ] = a [ i ] + c [ Math . floorDiv ( i , 2 ) ] ;
  }
  long ans = 0 ;
  for ( int i = n ;
  i > 0 ;
  i -- ) {
    for ( int j = ( int ) Math . pow ( 2 , i ) ;
    j < Math . pow ( 2 , i + 1 ) ;
    j += 2 ) {
      c [ Math . floorDiv ( j , 2 ) ] = Math . max ( c [ j ] , c [ j + 1 ] ) ;
      ans += Math . abs ( c [ j ] - c [ j + 1 ] ) ;
    }
  }
  System . out . println ( ans ) ;
}

public static void main ( String [ ] args ) { int n = in . nextInt ( ) ; int [ ] a = new int [ ( int ) Math . pow ( 2 , n + 1 ) ] ; int [ ] c = new int [ ( int ) Math . pow ( 2 , n + 1 ) ] ; for ( int i = 2 ; i < Math . pow ( 2 , n + 1 ) ; i ++ ) { a [ i ] = in . nextInt ( ) ; } for ( int i = 2 ; i < Math . pow ( 2 , n + 1 ) ; i ++ ) { c [ i ] = a [ i ] + c [ Math . floorDiv ( i , 2 ) ] ; } long ans = 0 ; for ( int i = n ; i > 0 ; i -- ) { for ( int j = ( int ) Math . pow ( 2 , i ) ; j < Math . pow ( 2 , i + 1 ) ; j += 2 ) { c [ Math . floorDiv ( j , 2 ) ] = Math . max ( c [ j ] , c [ j + 1 ] ) ; ans += Math . abs ( c [ j ] - c [ j + 1 ] ) ; } } System . out . println ( ans ) ; }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (array_type element: (type_identifier) dimensions: (dimensions)) name: (identifier))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (cast_expression type: (integral_type) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (binary_expression left: (identifier) right: (decimal_integer_literal))))))))) (local_variable_declaration type: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (cast_expression type: (integral_type) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (binary_expression left: (identifier) right: (decimal_integer_literal))))))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (binary_expression left: (identifier) right: (decimal_integer_literal))))) update: (update_expression (identifier)) body: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (binary_expression left: (identifier) right: (decimal_integer_literal))))) update: (update_expression (identifier)) body: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (decimal_integer_literal))))))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (identifier))) condition: (binary_expression left: (identifier) right: (decimal_integer_literal)) update: (update_expression (identifier)) body: (block (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (cast_expression type: (integral_type) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (identifier)))))) condition: (binary_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (binary_expression left: (identifier) right: (decimal_integer_literal))))) update: (assignment_expression left: (identifier) right: (decimal_integer_literal)) body: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (decimal_integer_literal)))) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (identifier) index: (identifier)) (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))))))) (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal)))))))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (identifier))))))