static int optimalSearchTree ( int keys [ ] , int freq [ ] , int n ) {
  int cost [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ;
  for ( int i = 0 ;
  i < n ;
  i ++ ) cost [ i ] [ i ] = freq [ i ] ;
  for ( int L = 2 ;
  L <= n ;
  L ++ ) {
    for ( int i = 0 ;
    i <= n - L + 1 ;
    i ++ ) {
      int j = i + L - 1 ;
      cost [ i ] [ j ] = Integer . MAX_VALUE ;
      for ( int r = i ;
      r <= j ;
      r ++ ) {
        int c = ( ( r > i ) ? cost [ i ] [ r - 1 ] : 0 ) + ( ( r < j ) ? cost [ r + 1 ] [ j ] : 0 ) + sum ( freq , i , j ) ;
        if ( c < cost [ i ] [ j ] ) cost [ i ] [ j ] = c ;
      }
    }
  }
  return cost [ 0 ] [ n - 1 ] ;
}

static int optimalSearchTree ( int keys [ ] , int freq [ ] , int n ) { int cost [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) cost [ i ] [ i ] = freq [ i ] ; for ( int L = 2 ; L <= n ; L ++ ) { for ( int i = 0 ; i <= n - L + 1 ; i ++ ) { int j = i + L - 1 ; cost [ i ] [ j ] = Integer . MAX_VALUE ; for ( int r = i ; r <= j ; r ++ ) { int c = ( ( r > i ) ? cost [ i ] [ r - 1 ] : 0 ) + ( ( r < j ) ? cost [ r + 1 ] [ j ] : 0 ) + sum ( freq , i , j ) ; if ( c < cost [ i ] [ j ] ) cost [ i ] [ j ] = c ; } } } return cost [ 0 ] [ n - 1 ] ; }
(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) dimensions: (dimensions)) (formal_parameter type: (integral_type) name: (identifier))) body: (block (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))) 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: (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (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: (identifier)) update: (update_expression (identifier)) body: (block (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal))) update: (update_expression (identifier)) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal)))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (field_access object: (identifier) field: (identifier)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (identifier))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (binary_expression left: (parenthesized_expression (ternary_expression condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (array_access array: (array_access array: (identifier) index: (identifier)) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) alternative: (decimal_integer_literal))) right: (parenthesized_expression (ternary_expression condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (identifier)) alternative: (decimal_integer_literal)))) right: (method_invocation name: (identifier) arguments: (argument_list (identifier) (identifier) (identifier)))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)))) consequence: (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (identifier)))))))))) (return_statement (array_access array: (array_access array: (identifier) index: (decimal_integer_literal)) index: (binary_expression left: (identifier) right: (decimal_integer_literal))))))