Cow Exhibition
InterviewTime limit1sMemory limit1024 MB
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 () cows a thorough interview and determined two values for each cow: the smartness () and the funness () of cow .
Bessie chooses which cows to bring to the exhibition. The total smartness of the group is the sum of the values, and the total funness is the sum of the values. Bessie wants to maximize , but both and must be non-negative. Help her maximize without letting either value become negative.
Input
The first line contains a single integer , the number of cows. Each of the next lines contains two space-separated integers and , the smartness and funness of cow .
Output
Print one integer: the maximum value of such that both and are non-negative. If no subset of cows has non-negative and non-negative , print .
Hint
Bessie chooses cows 1, 3, and 4, giving and , so . Adding cow 2 would raise to 10, but would become negative, so that choice is not allowed.