This page is still under construction.

Parts of this page are still being built. What you see may change.

Exciting Tournament

Time limit1sMemory limit512 MB

Summary
Given players with distinct skills and per-player game limits, choose any knockout bracket and report the minimum and maximum total XOR excitement over all games.
Level

Hard8 of 10

Topics
Dynamic programming, Greedy, Sorting, Bit manipulation
Solved
No attempts yet

Problem

A group of players compete in a no-holds-barred tournament.

Each player has a unique skill level, represented as an integer. In each game, two players play, and the player with the higher skill level wins. The player with the lower skill level is immediately eliminated from the tournament. The tournament continues until only one player is left.

Due to scheduling constraints, each player has a limit on the maximum number of games they can play. Interestingly, this is the only constraint that the tournament bracket needs to satisfy. In other words, the bracket does not necessarily have the shape of a balanced binary tree, as long as every player plays at most their maximum number of games before being eliminated or winning the entire tournament.

As the tournament organizer, you are free to choose any valid bracket. Given the list of participants, you wonder how exciting (or not exciting) the tournament can get. Concretely, the excitement of a game is defined as the bitwise XOR of the two players' skill levels. The excitement of the tournament is the sum of the excitement of each game.

Compute the minimum and maximum possible excitement values of the entire tournament.

Input

The first line of input contains a single integer nn (3≤n≤1003 \le n \le 100), which is the number of players in the tournament.

Each of the next nn lines contains two integers ss (0≤s<2300 \le s < 2^{30}) and gg (2≤g<n2 \le g < n). Each line describes a single player; ss is the skill level of the player, and gg is the limit on the number of games that player can play.

Output

Output two space-separated integers on a single line, which are the minimum and maximum possible excitement values of the entire tournament, minimum first.

Examples2

  1. Example 1

    Input
    4
    41 2
    13 2
    36 3
    17 3
    
    Expected output
    94 110
    
  2. Example 2

    Input
    6
    66 5
    628 4
    216 5
    78 4
    230 5
    74 3
    
    Expected output
    882 2650