#P5909. Longest Stable Pendant

    ID: 19135 Type: Default 1000ms 256MiB

Longest Stable Pendant

Longest Stable Pendant

A pendant is formed by connecting several beads sequentially. Each bead consists of three parts: the bead itself, the connection ring above, and the hook below. When forming the pendant, the connection is such that for the ith bead (with i > 1), its ring is attached to the hook of the (i-1)-th bead.

Each bead is characterized by two integers: its weight w and its hook capacity c. Due to gravity, every bead (except the bottom one) must support the total weight of all beads below it. Formally, if a pendant consists of beads b1, b2, ..., bk arranged from top to bottom, then the pendant is said to be stable if and only if for every i (1 ≤ i < k), the following condition holds:

[ \sum_{j=i+1}^{k} w_j \le c_i ]

Your task is to help Little Q choose some of the available beads to form a stable pendant that is as long as possible. If there are multiple ways to achieve the maximum length, choose the one with the smallest total weight.

Input and Output: You are given an integer N followed by N lines each containing two integers w and c that describe each bead’s weight and hook capacity respectively. Output two numbers: the maximum number of beads that can form a stable pendant and the minimal total weight of that pendant.

inputFormat

The first line contains a single integer N (1 ≤ N ≤ 105 or as constrained by the problem).

Each of the next N lines contains two integers w and c (0 ≤ w, c ≤ 109) — the weight of the bead and the hook capacity of that bead respectively.

outputFormat

Output two integers separated by a space: the maximum number of beads in a stable pendant and the minimum total weight of the pendant achieving that length.

sample

3
3 5
4 7
2 5
3 9