public static void main ( String [ ] args ) {
  Scanner sc = new Scanner ( System . in ) ;
  int N = sc . nextInt ( ) ;
  PriorityQueue < Node > minus = new PriorityQueue < > ( ( x , y ) -> x . a - y . a < 0 ? - 1 : 1 ) ;
  PriorityQueue < Node > plus = new PriorityQueue < > ( ( x , y ) -> y . a - x . a < 0 ? - 1 : 1 ) ;
  long zeroa = 0 ;
  for ( int i = 0 ;
  i < N ;
  ++ i ) {
    long a = sc . nextLong ( ) ;
    long b = sc . nextLong ( ) ;
    if ( a == b ) zeroa = Math . max ( zeroa , a ) ;
    else if ( a > b ) plus . add ( new Node ( a , b ) ) ;
    else minus . add ( new Node ( a , b ) ) ;
  }
  long ans = 0 ;
  long val = 0 ;
  while ( ! minus . isEmpty ( ) ) {
    Node n = minus . poll ( ) ;
    ans = Math . max ( ans , val + n . a ) ;
    val += n . sub ;
  }
  ans = Math . max ( ans , val + zeroa ) ;
  long max = 0 ;
  long pval = 0 ;
  while ( ! plus . isEmpty ( ) ) {
    Node n = plus . poll ( ) ;
    if ( pval + n . a > max ) max = Math . min ( pval + n . a , Math . max ( max + n . sub , n . a ) ) ;
    pval += n . sub ;
  }
  System . out . println ( Math . max ( ans , val + max ) ) ;
}

public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int N = sc . nextInt ( ) ; PriorityQueue < Node > minus = new PriorityQueue < > ( ( x , y ) -> x . a - y . a < 0 ? - 1 : 1 ) ; PriorityQueue < Node > plus = new PriorityQueue < > ( ( x , y ) -> y . a - x . a < 0 ? - 1 : 1 ) ; long zeroa = 0 ; for ( int i = 0 ; i < N ; ++ i ) { long a = sc . nextLong ( ) ; long b = sc . nextLong ( ) ; if ( a == b ) zeroa = Math . max ( zeroa , a ) ; else if ( a > b ) plus . add ( new Node ( a , b ) ) ; else minus . add ( new Node ( a , b ) ) ; } long ans = 0 ; long val = 0 ; while ( ! minus . isEmpty ( ) ) { Node n = minus . poll ( ) ; ans = Math . max ( ans , val + n . a ) ; val += n . sub ; } ans = Math . max ( ans , val + zeroa ) ; long max = 0 ; long pval = 0 ; while ( ! plus . isEmpty ( ) ) { Node n = plus . poll ( ) ; if ( pval + n . a > max ) max = Math . min ( pval + n . a , Math . max ( max + n . sub , n . a ) ) ; pval += n . sub ; } System . out . println ( Math . max ( ans , val + max ) ) ; }
(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 (field_access object: (identifier) field: (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: (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 (lambda_expression parameters: (inferred_parameters (identifier) (identifier)) body: (ternary_expression condition: (binary_expression left: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (field_access object: (identifier) field: (identifier))) right: (decimal_integer_literal)) consequence: (unary_expression operand: (decimal_integer_literal)) alternative: (decimal_integer_literal))))))) (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 (lambda_expression parameters: (inferred_parameters (identifier) (identifier)) body: (ternary_expression condition: (binary_expression left: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (field_access object: (identifier) field: (identifier))) right: (decimal_integer_literal)) consequence: (unary_expression operand: (decimal_integer_literal)) alternative: (decimal_integer_literal))))))) (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 (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)))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (identifier))))) alternative: (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (object_creation_expression type: (type_identifier) arguments: (argument_list (identifier) (identifier)))))) alternative: (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (object_creation_expression type: (type_identifier) arguments: (argument_list (identifier) (identifier)))))))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (while_statement condition: (parenthesized_expression (unary_expression operand: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) 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: (field_access object: (identifier) field: (identifier))))))) (expression_statement (assignment_expression left: (identifier) right: (field_access object: (identifier) field: (identifier)))))) (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (identifier) right: (identifier)))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (while_statement condition: (parenthesized_expression (unary_expression operand: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (field_access object: (identifier) field: (identifier))) right: (identifier))) consequence: (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (field_access object: (identifier) field: (identifier))) (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (field_access object: (identifier) field: (identifier))) (field_access object: (identifier) field: (identifier))))))))) (expression_statement (assignment_expression left: (identifier) right: (field_access object: (identifier) field: (identifier)))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (identifier) right: (identifier)))))))))