Under construction

Still being built. Things may change

Roller Coaster

Interview

Time limit1sMemory limit128 MB

Summary
Each section of a roller coaster either adds fun and dizziness or reduces dizziness by K; find the maximum fun keeping dizziness at or below L at all times.
Level

Medium6 of 10

Topics
Dynamic programming
Solved
No attempts yet

Problem

Bessie has gone on a trip, and she is riding a roller coaster! Bessie really likes riding roller coasters, but unfortunately she often gets dizzy.

The roller coaster has a number of distinct sections that Bessie rides in order. At the start of the ride, both her dizziness and her fun are 0. For each section of the roller coaster, Bessie can either keep her eyes open or keep them closed, and she must keep them that way for the whole section.

  • If she keeps her eyes open for a section, her total fun increases by that section's fun factor and her dizziness increases by that section's dizziness factor.
  • If she keeps her eyes closed for a section, her total fun does not change, but her dizziness decreases by a value that is constant for the entire roller coaster. Her dizziness can never go below 0 (if a decrease would take it below 0, it becomes 0).

If at any point Bessie's dizziness is above a certain limit, she will get sick. Find the maximum amount of fun Bessie can have without getting sick.

Input

The input contains several test cases. Each test case begins with a line containing three integers:

N K L

where NN (1≤N≤1,0001 \le N \le 1{,}000) is the number of sections in this roller coaster, KK (1≤K≤5001 \le K \le 500) is the amount Bessie's dizziness goes down if she keeps her eyes closed on any section, and LL (1≤L≤300,0001 \le L \le 300{,}000) is the dizziness limit Bessie can tolerate — if her dizziness ever becomes larger than LL, Bessie gets sick.

Each of the next NN lines describes one section of the roller coaster and contains two integers:

F D

where FF (1≤F≤201 \le F \le 20) is the increase to Bessie's total fun if she keeps her eyes open on that section, and DD (1≤D≤5001 \le D \le 500) is the increase to her dizziness if she keeps her eyes open on that section. The sections are listed in ride order. The input ends with a line containing three 0s.

Output

For each test case, output a single integer: the maximum amount of fun Bessie can have on that roller coaster without exceeding her dizziness limit. Print each integer on its own line with no spaces, and do not print any blank lines between answers.

Examples1

  1. Example 1

    Input
    3 1 2
    2 1
    3 1
    5 2
    10 5 1
    20 2
    12 4
    3 3
    10 6
    20 3
    19 9
    19 7
    1 500
    15 5
    4 2
    0 0 0
    
    Expected output
    7
    0