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 ) ;
}

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 ) ; }
(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: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (field_access object: (identifier) field: (identifier)))))) (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: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (cast_expression type: (integral_type) value: (parenthesized_expression (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (binary_expression left: (identifier) right: (decimal_integer_literal)))))) right: (decimal_integer_literal)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (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: (identifier)) 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)))))) (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: (binary_expression left: (cast_expression type: (integral_type) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (identifier)))) right: (decimal_integer_literal)))) condition: (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 (array_access array: (identifier) index: (binary_expression left: (decimal_integer_literal) right: (identifier))) (array_access array: (identifier) index: (binary_expression left: (binary_expression left: (decimal_integer_literal) right: (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: (binary_expression left: (decimal_integer_literal) right: (identifier))) right: (array_access array: (identifier) index: (binary_expression left: (binary_expression left: (decimal_integer_literal) right: (identifier)) right: (decimal_integer_literal)))))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (identifier))))))