#P2340. [USACO03FALL] Cow Exhibition G

[USACO03FALL] Cow Exhibition G

Description

The cows want to prove that they are smart and funny. To this end, Bessie is organizing a cow exhibition. She has interviewed NN cows and determined each cow’s smartness and fun.

Bessie may choose which cows to include. Because negative smartness or fun would have adverse effects, she does not want the total smartness to be negative, nor the total fun to be negative. Subject to both constraints, she wants to maximize the sum of smartness and fun over the selected cows. Help Bessie compute this maximum.

Input Format

The first line contains a single integer NN (1N4001 \le N \le 400).

Lines 22 through N+1N+1: the (i+1)(i+1)-th line contains two integers SiS_i and FiF_i, the smartness and fun of the ii-th cow (1000Si,Fi1000-1000 \le S_i, F_i \le 1000).

Output Format

Output a single integer: the maximum possible sum of smartness and fun. Bessie may choose no cows; if that is optimal, output 00.

5
-5 7
8 -6
6 -3
2 1
-8 -5
8

Hint

Select cows 11, 33, and 44: the smartness sum is 5+6+2=3-5 + 6 + 2 = 3, and the fun sum is 73+1=57 - 3 + 1 = 5.

Adding cow 22 would raise the total to 1010, but the fun sum would become negative, which is not allowed.

Translated by ChatGPT 5