Roller Coaster

Interview

Time limit2sMemory limit128 MB

Summary
Choose open or closed eyes for each roller coaster section to maximize total fun while keeping dizziness within limit L, where closing decreases dizziness by K.
Level

Medium6 of 10

Topics
Dynamic programming, Greedy, Implementation, Array
Solved
No attempts yet

Problem

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

The roller coaster is made up of several distinct sections, which Bessie rides in order. At the start of the ride, both Bessie's dizziness and fun are 0. For each section, Bessie can keep her eyes either open or closed, and she must hold that choice 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 fun does not change, but her dizziness decreases by a value that is fixed for the whole roller coaster. (Her dizziness can never drop below 0.)

If at any moment Bessie's dizziness rises above a certain limit, she gets sick. Write a program to 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 of three integers.

N K L
  • NN (1≤N≤1,0001 \le N \le 1{,}000): the number of sections in this roller coaster
  • KK (1≤K≤5001 \le K \le 500): the amount Bessie's dizziness decreases when she keeps her eyes closed on a section
  • LL (1≤L≤300,0001 \le L \le 300{,}000): the dizziness limit Bessie can tolerate. If her dizziness ever exceeds LL, she gets sick.

Each of the next NN lines contains two integers.

F D
  • FF (1≤F≤201 \le F \le 20): the fun gained if she keeps her eyes open on that section
  • DD (1≤D≤5001 \le D \le 500): the dizziness gained if she keeps her eyes open on that section

The sections are given in order. The input ends with a line containing three 0s, which should not be processed.

Output

For each test case, output a single integer on its own line: the maximum amount of fun Bessie can have without exceeding her dizziness limit.

Examples1

  1. Example 1

    Input
    3 1 2
    2 1
    3 1
    5 2
    4 1 1
    2 1
    3 1
    2 2
    3 3
    0 0 0
    
    Expected output
    7
    3