#P8806. [蓝桥杯 2022 国 B] 搬砖

[蓝桥杯 2022 国 B] 搬砖

Problem Description

On this day, Xiaoming is moving bricks.

He has a total of nn bricks. He finds that the weight of the ii-th brick is wiw_{i} and its value is viv_{i}. He suddenly wants to choose some of these bricks and stack them from bottom to top into a tower. For each brick in the tower, the sum of the weights of all bricks above it must not exceed its own value.

He wants to know what the maximum possible total value of such a tower is (that is, the sum of the values of all bricks in the tower).

Input Format

The input has n+1n+1 lines. The first line contains a positive integer nn, which represents the number of bricks.

The next nn lines each contain two positive integers wi,viw_{i}, v_{i}, representing the weight and value of each brick.

Output Format

One line, an integer representing the answer.

5
4 4
1 1
5 2
5 5
4 3
10

Hint

Sample Explanation

Choose bricks 11, 22, and 44. Stack them from top to bottom in the order 22, 11, 44. The total value is 4+1+5=104+1+5=10.

Constraints and Notes

For 20%20\% of the testdata, it is guaranteed that n10n \leq 10.

For 100%100\% of the testdata, it is guaranteed that n1000n \leq 1000; wi20w_{i} \leq 20; vi20000v_{i} \leq 20000.

Lanqiao Cup 2022 National Contest B Group, Problem J.

Translated by ChatGPT 5