public static void main ( String [ ] args ) {
  Scanner in = new Scanner ( System . in ) ;
  int n = in . nextInt ( ) , i , a [ ] = new int [ n ] , b [ ] = new int [ n ] , c = 0 , l = 0 ;
  long s = 0 ;
  for ( i = 0 ;
  i < n ;
  i ++ ) {
    a [ i ] = in . nextInt ( ) ;
    b [ i ] = in . nextInt ( ) ;
    s += b [ i ] ;
  }
  in . close ( ) ;
  while ( c < s ) {
    int x = Integer . MAX_VALUE , y = Integer . MIN_VALUE , xi = 0 , yi = 0 , xn = 0 , yn = 0 ;
    for ( i = 0 ;
    i < n ;
    i ++ ) {
      if ( a [ i ] > 0 ) {
        if ( a [ i ] < x ) {
          x = a [ i ] ;
          xi = i ;
        }
        xn ++ ;
      }
      else if ( a [ i ] < 0 ) {
        if ( a [ i ] > y ) {
          y = a [ i ] ;
          yi = i ;
        }
        yn ++ ;
      }
    }
    if ( xn == 0 && yn == 0 ) break ;
    if ( l == 0 && xn >= yn || l < 0 && xn > 0 ) {
      c += b [ xi ] ;
      l = x ;
      a [ xi ] = 0 ;
    }
    else if ( l == 0 && xn < yn || l > 0 && yn > 0 ) {
      c += b [ yi ] ;
      l = y ;
      a [ yi ] = 0 ;
    }
    else {
      break ;
    }
  }
  System . out . println ( c ) ;
}

public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; int n = in . nextInt ( ) , i , a [ ] = new int [ n ] , b [ ] = new int [ n ] , c = 0 , l = 0 ; long s = 0 ; for ( i = 0 ; i < n ; i ++ ) { a [ i ] = in . nextInt ( ) ; b [ i ] = in . nextInt ( ) ; s += b [ i ] ; } in . close ( ) ; while ( c < s ) { int x = Integer . MAX_VALUE , y = Integer . MIN_VALUE , xi = 0 , yi = 0 , xn = 0 , yn = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] > 0 ) { if ( a [ i ] < x ) { x = a [ i ] ; xi = i ; } xn ++ ; } else if ( a [ i ] < 0 ) { if ( a [ i ] > y ) { y = a [ i ] ; yi = i ; } yn ++ ; } } if ( xn == 0 && yn == 0 ) break ; if ( l == 0 && xn >= yn || l < 0 && xn > 0 ) { c += b [ xi ] ; l = x ; a [ xi ] = 0 ; } else if ( l == 0 && xn < yn || l > 0 && yn > 0 ) { c += b [ yi ] ; l = y ; a [ yi ] = 0 ; } else { break ; } } System . out . println ( c ) ; }
(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))) declarator: (variable_declarator name: (identifier)) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (identifier)))) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (identifier)))) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal)) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (for_statement init: (assignment_expression left: (identifier) right: (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: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (expression_statement (assignment_expression left: (identifier) right: (array_access array: (identifier) index: (identifier)))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))) (while_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (field_access object: (identifier) field: (identifier))) declarator: (variable_declarator name: (identifier) value: (field_access object: (identifier) field: (identifier))) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal)) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal)) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal)) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (for_statement init: (assignment_expression left: (identifier) right: (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: (identifier)) right: (decimal_integer_literal))) consequence: (block (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (identifier))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (array_access array: (identifier) index: (identifier)))) (expression_statement (assignment_expression left: (identifier) right: (identifier))))) (expression_statement (update_expression (identifier)))) alternative: (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (decimal_integer_literal))) consequence: (block (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (identifier))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (array_access array: (identifier) index: (identifier)))) (expression_statement (assignment_expression left: (identifier) right: (identifier))))) (expression_statement (update_expression (identifier)))))))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (decimal_integer_literal)) right: (binary_expression left: (identifier) right: (decimal_integer_literal)))) consequence: (break_statement)) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (binary_expression left: (identifier) right: (decimal_integer_literal)) right: (binary_expression left: (identifier) right: (identifier))) right: (binary_expression left: (binary_expression left: (identifier) right: (decimal_integer_literal)) right: (binary_expression left: (identifier) right: (decimal_integer_literal))))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (array_access array: (identifier) index: (identifier)))) (expression_statement (assignment_expression left: (identifier) right: (identifier))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (decimal_integer_literal)))) alternative: (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (binary_expression left: (identifier) right: (decimal_integer_literal)) right: (binary_expression left: (identifier) right: (identifier))) right: (binary_expression left: (binary_expression left: (identifier) right: (decimal_integer_literal)) right: (binary_expression left: (identifier) right: (decimal_integer_literal))))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (array_access array: (identifier) index: (identifier)))) (expression_statement (assignment_expression left: (identifier) right: (identifier))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (decimal_integer_literal)))) alternative: (block (break_statement)))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (identifier))))))