public static void main ( String [ ] $ ) {
  Scanner sc = new Scanner ( in ) ;
  int n = sc . nextInt ( ) ;
  int k = sc . nextInt ( ) ;
  long [ ] f = new long [ k + 1 ] ;
  Point [ ] sushi = new Point [ n ] ;
  for ( int i = 0 ;
  i < n ;
  i ++ ) {
    sushi [ i ] = ( new Point ( sc . nextInt ( ) , sc . nextInt ( ) ) ) ;
  }
  HashSet < Integer > S = new HashSet < > ( ) ;
  Arrays . sort ( sushi , ( p1 , p2 ) -> p2 . y - p1 . y ) ;
  long temp = 0 ;
  int [ ] s = new int [ n + 1 ] ;
  for ( int i = 0 ;
  i < k ;
  i ++ ) {
    temp += ( long ) sushi [ i ] . y ;
    S . add ( sushi [ i ] . x ) ;
    s [ sushi [ i ] . x ] ++ ;
  }
  int size = S . size ( ) ;
  f [ size ] = temp ;
  int left = k - 1 , right = k ;
  while ( 0 <= left && right < n ) {
    while ( 0 <= left && s [ sushi [ left ] . x ] == 1 ) {
      left -- ;
    }
    while ( right < n && s [ sushi [ right ] . x ] > 0 ) {
      right ++ ;
    }
    if ( 0 <= left && right < n ) {
      f [ size + 1 ] = f [ size ] - sushi [ left ] . y + sushi [ right ] . y ;
      size ++ ;
      s [ sushi [ right ] . x ] ++ ;
      s [ sushi [ left ] . x ] -- ;
      left -- ;
    }
  }
  long ans = 0 ;
  for ( long i = 1 ;
  i <= k ;
  i ++ ) {
    if ( f [ ( int ) i ] > 0 ) ans = max ( ans , f [ ( int ) i ] + i * i ) ;
  }
  out . println ( ans ) ;
}

public static void main ( String [ ] $ ) { Scanner sc = new Scanner ( in ) ; int n = sc . nextInt ( ) ; int k = sc . nextInt ( ) ; long [ ] f = new long [ k + 1 ] ; Point [ ] sushi = new Point [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { sushi [ i ] = ( new Point ( sc . nextInt ( ) , sc . nextInt ( ) ) ) ; } HashSet < Integer > S = new HashSet < > ( ) ; Arrays . sort ( sushi , ( p1 , p2 ) -> p2 . y - p1 . y ) ; long temp = 0 ; int [ ] s = new int [ n + 1 ] ; for ( int i = 0 ; i < k ; i ++ ) { temp += ( long ) sushi [ i ] . y ; S . add ( sushi [ i ] . x ) ; s [ sushi [ i ] . x ] ++ ; } int size = S . size ( ) ; f [ size ] = temp ; int left = k - 1 , right = k ; while ( 0 <= left && right < n ) { while ( 0 <= left && s [ sushi [ left ] . x ] == 1 ) { left -- ; } while ( right < n && s [ sushi [ right ] . x ] > 0 ) { right ++ ; } if ( 0 <= left && right < n ) { f [ size + 1 ] = f [ size ] - sushi [ left ] . y + sushi [ right ] . y ; size ++ ; s [ sushi [ right ] . x ] ++ ; s [ sushi [ left ] . x ] -- ; left -- ; } } long ans = 0 ; for ( long i = 1 ; i <= k ; i ++ ) { if ( f [ ( int ) i ] > 0 ) ans = max ( ans , f [ ( int ) i ] + i * i ) ; } 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 (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (identifier))))) (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: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (binary_expression left: (identifier) right: (decimal_integer_literal)))))) (local_variable_declaration type: (array_type element: (type_identifier) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (type_identifier) dimensions: (dimensions_expr (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 (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (parenthesized_expression (object_creation_expression type: (type_identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))))))))) (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)) arguments: (argument_list)))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (lambda_expression parameters: (inferred_parameters (identifier) (identifier)) body: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (field_access object: (identifier) field: (identifier))))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (integral_type) 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: (block (expression_statement (assignment_expression left: (identifier) right: (cast_expression type: (integral_type) value: (field_access object: (array_access array: (identifier) index: (identifier)) field: (identifier))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (field_access object: (array_access array: (identifier) index: (identifier)) field: (identifier))))) (expression_statement (update_expression (array_access array: (identifier) index: (field_access object: (array_access array: (identifier) index: (identifier)) field: (identifier))))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (identifier))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (identifier) right: (decimal_integer_literal))) declarator: (variable_declarator name: (identifier) value: (identifier))) (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (decimal_integer_literal) right: (identifier)) right: (binary_expression left: (identifier) right: (identifier)))) body: (block (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (decimal_integer_literal) right: (identifier)) right: (binary_expression left: (array_access array: (identifier) index: (field_access object: (array_access array: (identifier) index: (identifier)) field: (identifier))) right: (decimal_integer_literal)))) body: (block (expression_statement (update_expression (identifier))))) (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (binary_expression left: (array_access array: (identifier) index: (field_access object: (array_access array: (identifier) index: (identifier)) field: (identifier))) right: (decimal_integer_literal)))) body: (block (expression_statement (update_expression (identifier))))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (decimal_integer_literal) right: (identifier)) right: (binary_expression left: (identifier) right: (identifier)))) consequence: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (binary_expression left: (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (field_access object: (array_access array: (identifier) index: (identifier)) field: (identifier))) right: (field_access object: (array_access array: (identifier) index: (identifier)) field: (identifier))))) (expression_statement (update_expression (identifier))) (expression_statement (update_expression (array_access array: (identifier) index: (field_access object: (array_access array: (identifier) index: (identifier)) field: (identifier))))) (expression_statement (update_expression (array_access array: (identifier) index: (field_access object: (array_access array: (identifier) index: (identifier)) field: (identifier))))) (expression_statement (update_expression (identifier))))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (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 (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (cast_expression type: (integral_type) value: (identifier))) right: (decimal_integer_literal))) consequence: (expression_statement (assignment_expression left: (identifier) right: (method_invocation name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (array_access array: (identifier) index: (cast_expression type: (integral_type) value: (identifier))) right: (binary_expression left: (identifier) right: (identifier)))))))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))))))