private static int solve ( int d , int g , int [ ] p , int [ ] c , int used ) {
  if ( memo [ g ] [ used ] != 0 ) return memo [ g ] [ used ] ;
  int res = INF ;
  for ( int i = 0 ;
  i < d ;
  i ++ ) {
    int bit = ( int ) Math . pow ( 2 , i ) ;
    if ( ( used / bit ) % 2 == 1 ) continue ;
    if ( ( i + 1 ) * ( p [ i ] - 1 ) >= g ) {
      res = Math . min ( res , ( int ) Math . ceil ( 1.0 * g / ( i + 1 ) ) ) ;
    }
    else if ( ( i + 1 ) * p [ i ] + c [ i ] >= g ) {
      res = Math . min ( res , p [ i ] ) ;
    }
    else {
      res = Math . min ( res , p [ i ] + solve ( d , g - ( ( i + 1 ) * p [ i ] + c [ i ] ) , p , c , used + bit ) ) ;
    }
  }
  memo [ g ] [ used ] = res ;
  return res ;
}

private static int solve ( int d , int g , int [ ] p , int [ ] c , int used ) { if ( memo [ g ] [ used ] != 0 ) return memo [ g ] [ used ] ; int res = INF ; for ( int i = 0 ; i < d ; i ++ ) { int bit = ( int ) Math . pow ( 2 , i ) ; if ( ( used / bit ) % 2 == 1 ) continue ; if ( ( i + 1 ) * ( p [ i ] - 1 ) >= g ) { res = Math . min ( res , ( int ) Math . ceil ( 1.0 * g / ( i + 1 ) ) ) ; } else if ( ( i + 1 ) * p [ i ] + c [ i ] >= g ) { res = Math . min ( res , p [ i ] ) ; } else { res = Math . min ( res , p [ i ] + solve ( d , g - ( ( i + 1 ) * p [ i ] + c [ i ] ) , p , c , used + bit ) ) ; } } memo [ g ] [ used ] = res ; return res ; }
(method_declaration (modifiers) type: (integral_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (integral_type) name: (identifier)) (formal_parameter type: (integral_type) name: (identifier)) (formal_parameter type: (array_type element: (integral_type) dimensions: (dimensions)) name: (identifier)) (formal_parameter type: (array_type element: (integral_type) dimensions: (dimensions)) name: (identifier)) (formal_parameter type: (integral_type) name: (identifier))) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (decimal_integer_literal))) consequence: (return_statement (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (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 (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)))))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) right: (decimal_integer_literal)) right: (decimal_integer_literal))) consequence: (continue_statement)) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (decimal_integer_literal)))) right: (identifier))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (cast_expression type: (integral_type) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (binary_expression left: (decimal_floating_point_literal) right: (identifier)) right: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal)))))))))))) alternative: (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (array_access array: (identifier) index: (identifier))) right: (array_access array: (identifier) index: (identifier))) right: (identifier))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (array_access array: (identifier) index: (identifier))))))) alternative: (block (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (method_invocation name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (identifier) right: (parenthesized_expression (binary_expression left: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (array_access array: (identifier) index: (identifier))) right: (array_access array: (identifier) index: (identifier))))) (identifier) (identifier) (binary_expression left: (identifier) right: (identifier)))))))))))))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (identifier))) (return_statement (identifier))))