static int getMinCost ( int arr [ ] , int n , int cost [ ] ) {
  Map < Integer , Integer > mp = new ConcurrentHashMap < Integer , Integer > ( ) ;
  mp . clear ( ) ;
  mp . put ( 0 , 0 ) ;
  for ( int i = 0 ;
  i < n ;
  i ++ ) {
    for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) {
      int gcd = __gcd ( arr [ i ] , it . getKey ( ) ) ;
      if ( mp . containsKey ( gcd ) ) mp . put ( gcd , Math . min ( mp . get ( gcd ) , it . getValue ( ) + cost [ i ] ) ) ;
      else mp . put ( gcd , it . getValue ( ) + cost [ i ] ) ;
    }
  }
  if ( ! mp . containsKey ( 1 ) ) return - 1 ;
  else return mp . get ( 1 ) ;
}

static int getMinCost ( int arr [ ] , int n , int cost [ ] ) { Map < Integer , Integer > mp = new ConcurrentHashMap < Integer , Integer > ( ) ; mp . clear ( ) ; mp . put ( 0 , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { int gcd = __gcd ( arr [ i ] , it . getKey ( ) ) ; if ( mp . containsKey ( gcd ) ) mp . put ( gcd , Math . min ( mp . get ( gcd ) , it . getValue ( ) + cost [ i ] ) ) ; else mp . put ( gcd , it . getValue ( ) + cost [ i ] ) ; } } if ( ! mp . containsKey ( 1 ) ) return - 1 ; else return mp . get ( 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)) (formal_parameter type: (integral_type) name: (identifier) dimensions: (dimensions))) body: (block (local_variable_declaration type: (generic_type (type_identifier) (type_arguments (type_identifier) (type_identifier))) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (generic_type (type_identifier) (type_arguments (type_identifier) (type_identifier))) arguments: (argument_list)))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (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 (enhanced_for_statement type: (generic_type (scoped_type_identifier (type_identifier) (type_identifier)) (type_arguments (type_identifier) (type_identifier))) name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation name: (identifier) arguments: (argument_list (array_access array: (identifier) index: (identifier)) (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))))) (if_statement condition: (parenthesized_expression (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))) consequence: (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (array_access array: (identifier) index: (identifier)))))))) alternative: (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (array_access array: (identifier) index: (identifier))))))))))) (if_statement condition: (parenthesized_expression (unary_expression operand: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal))))) consequence: (return_statement (unary_expression operand: (decimal_integer_literal))) alternative: (return_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal)))))))