public static void main ( String [ ] args ) {
  for ( int i = 0 ;
  i < N ;
  i ++ ) {
    int t = sc . nextInt ( ) ;
    long d = sc . nextLong ( ) ;
    sushi su = new sushi ( t , d ) ;
    S [ i ] = su ;
  }
  Arrays . sort ( S , Comparator . comparing ( sushi :: getD ) . reversed ( ) ) ;
  for ( int i = 0 ;
  i < K ;
  i ++ ) {
    if ( used [ S [ i ] . t ] ) {
      st . push ( S [ i ] . d ) ;
    }
    else {
      used [ S [ i ] . t ] = true ;
      sum_t ++ ;
    }
    sum_d += S [ i ] . d ;
  }
  ans = sum_d + sum_t * sum_t ;
  for ( int i = K ;
  i < N ;
  i ++ ) {
    if ( st . empty ( ) ) break ;
    if ( used [ S [ i ] . t ] ) continue ;
    used [ S [ i ] . t ] = true ;
    sum_t ++ ;
    sum_d += S [ i ] . d - ( long ) st . pop ( ) ;
    ans = Math . max ( ans , sum_d + sum_t * sum_t ) ;
  }
  System . out . println ( ans ) ;
}

public static void main ( String [ ] args ) { for ( int i = 0 ; i < N ; i ++ ) { int t = sc . nextInt ( ) ; long d = sc . nextLong ( ) ; sushi su = new sushi ( t , d ) ; S [ i ] = su ; } Arrays . sort ( S , Comparator . comparing ( sushi :: getD ) . reversed ( ) ) ; for ( int i = 0 ; i < K ; i ++ ) { if ( used [ S [ i ] . t ] ) { st . push ( S [ i ] . d ) ; } else { used [ S [ i ] . t ] = true ; sum_t ++ ; } sum_d += S [ i ] . d ; } ans = sum_d + sum_t * sum_t ; for ( int i = K ; i < N ; i ++ ) { if ( st . empty ( ) ) break ; if ( used [ S [ i ] . t ] ) continue ; used [ S [ i ] . t ] = true ; sum_t ++ ; sum_d += S [ i ] . d - ( long ) st . pop ( ) ; ans = Math . max ( ans , sum_d + sum_t * sum_t ) ; } 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 (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: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (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: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (identifier) (identifier))))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (identifier))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (method_reference (identifier) (identifier)))) name: (identifier) arguments: (argument_list))))) (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 (if_statement condition: (parenthesized_expression (array_access array: (identifier) index: (field_access object: (array_access array: (identifier) index: (identifier)) field: (identifier)))) consequence: (block (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (field_access object: (array_access array: (identifier) index: (identifier)) field: (identifier)))))) alternative: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (field_access object: (array_access array: (identifier) index: (identifier)) field: (identifier))) right: (true))) (expression_statement (update_expression (identifier))))) (expression_statement (assignment_expression left: (identifier) right: (field_access object: (array_access array: (identifier) index: (identifier)) field: (identifier)))))) (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (identifier) right: (binary_expression left: (identifier) right: (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 (if_statement condition: (parenthesized_expression (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))) consequence: (break_statement)) (if_statement condition: (parenthesized_expression (array_access array: (identifier) index: (field_access object: (array_access array: (identifier) index: (identifier)) field: (identifier)))) consequence: (continue_statement)) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (field_access object: (array_access array: (identifier) index: (identifier)) field: (identifier))) right: (true))) (expression_statement (update_expression (identifier))) (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (field_access object: (array_access array: (identifier) index: (identifier)) field: (identifier)) right: (cast_expression type: (integral_type) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))))) (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (identifier) right: (binary_expression left: (identifier) right: (identifier))))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (identifier))))))