static int MinCostTree ( int arr [ ] , int n ) {
  int ans = 0 ;
  Vector < Integer > st = new Vector < Integer > ( ) ;
  st . add ( Integer . MAX_VALUE ) ;
  for ( int i = 0 ;
  i < n ;
  i ++ ) {
    while ( st . get ( st . size ( ) - 1 ) <= arr [ i ] ) {
      int x = st . get ( st . size ( ) - 1 ) ;
      st . remove ( st . size ( ) - 1 ) ;
      ans += x * Math . min ( st . get ( st . size ( ) - 1 ) , arr [ i ] ) ;
    }
    st . add ( arr [ i ] ) ;
  }
  for ( int i = 2 ;
  i < st . size ( ) ;
  i ++ ) ans += st . get ( i ) * st . get ( i - 1 ) ;
  return ans ;
}

static int MinCostTree ( int arr [ ] , int n ) { int ans = 0 ; Vector < Integer > st = new Vector < Integer > ( ) ; st . add ( Integer . MAX_VALUE ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( st . get ( st . size ( ) - 1 ) <= arr [ i ] ) { int x = st . get ( st . size ( ) - 1 ) ; st . remove ( st . size ( ) - 1 ) ; ans += x * Math . min ( st . get ( st . size ( ) - 1 ) , arr [ i ] ) ; } st . add ( arr [ i ] ) ; } for ( int i = 2 ; i < st . size ( ) ; i ++ ) ans += st . get ( i ) * st . get ( i - 1 ) ; return ans ; }
(method_declaration (modifiers) type: (integral_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (integral_type) name: (identifier) dimensions: (dimensions)) (formal_parameter type: (integral_type) name: (identifier))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (generic_type (type_identifier) (type_arguments (type_identifier))) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (generic_type (type_identifier) (type_arguments (type_identifier))) arguments: (argument_list)))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (field_access object: (identifier) field: (identifier))))) (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 (while_statement condition: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))) right: (array_access array: (identifier) index: (identifier)))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal))))) (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))) (array_access array: (identifier) index: (identifier))))))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (identifier) index: (identifier))))))) (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))) update: (update_expression (identifier)) body: (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)))))))) (return_statement (identifier))))