Under construction

Still being built. Things may change

Cow Exhibition

Interview

Time limit1sMemory limit1024 MB

Summary
Choose a subset of cows so that total smartness and total funness are both non-negative, and maximize their sum.
Level

Medium5 of 10

Topics
Dynamic programming, Array
Solved
No attempts yet

Problem

"Fat and docile, big and dumb, they look so stupid, they aren't much fun..."

  • Cows with Guns by Dana Lyons

The cows want to prove to the public that they are both smart and fun. To do this, Bessie has organized an exhibition that the cows will put on. She has given each of the NN (1≤N≤1001 \le N \le 100) cows a thorough interview and determined two values for each cow: the smartness SiS_i (−1000≤Si≤1000-1000 \le S_i \le 1000) and the funness FiF_i (−1000≤Fi≤1000-1000 \le F_i \le 1000) of cow ii.

Bessie chooses which cows to bring to the exhibition. The total smartness TSTS of the group is the sum of the SiS_i values, and the total funness TFTF is the sum of the FiF_i values. Bessie wants to maximize TS+TFTS + TF, but both TSTS and TFTF must be non-negative. Help her maximize TS+TFTS + TF without letting either value become negative.

Input

The first line contains a single integer NN, the number of cows. Each of the next NN lines contains two space-separated integers SiS_i and FiF_i, the smartness and funness of cow ii.

Output

Print one integer: the maximum value of TS+TFTS + TF such that both TSTS and TFTF are non-negative. If no subset of cows has non-negative TSTS and non-negative TFTF, print 00.

Hint

Bessie chooses cows 1, 3, and 4, giving TS=−5+6+2=3TS = -5 + 6 + 2 = 3 and TF=7−3+1=5TF = 7 - 3 + 1 = 5, so TS+TF=8TS + TF = 8. Adding cow 2 would raise TS+TFTS + TF to 10, but TFTF would become negative, so that choice is not allowed.

Examples1

  1. Example 1

    Input
    5
    -5 7
    8 -6
    6 -3
    2 1
    -8 -5
    
    Expected output
    8