This page is still under construction.

Parts of this page are still being built. What you see may change.

IOI Manju

Interview

Time limit1sMemory limit256 MB

Summary
Choose boxes and fill them with the priciest manju so packed value minus box cost is as large as possible.
Level

Medium6 of 10

Topics
Dynamic programming, Sorting, Prefix sum
Solved
No attempts yet

Problem

IOI Inc. made MM distinct IOI manju. Manju ii costs PiP_i yen (1≤i≤M1 \le i \le M).

JOI Inc. offers NN box types. Box jj (1≤j≤N1 \le j \le N) holds up to CjC_j manju and costs EjE_j yen. IOI orders between 0 and NN box types, one of each chosen type, packs manju into them, and sells each packed set for the sum of manju prices inside.

If every set sells, what is the maximum profit (total manju sales minus total box costs)? Manju left unpacked do not affect profit.

Input

  • Line 1: MM, NN.
  • Next MM lines: PiP_i.
  • Next NN lines: CjC_j, EjE_j.

Output

One integer: maximum profit.

Constraints

  • 1≤M≤10 0001 \le M \le 10\,000.
  • 1≤N≤5001 \le N \le 500.
  • 1≤Pi≤10 0001 \le P_i \le 10\,000.
  • 1≤Cj≤10 0001 \le C_j \le 10\,000.
  • 1≤Ej≤10 0001 \le E_j \le 10\,000.

Examples5

  1. Example 1

    Input
    4 3
    180
    160
    170
    190
    2 100
    3 120
    4 250
    
    Expected output
    480
    
  2. Example 2

    Input
    2 2
    1000
    2000
    1 6666
    1 7777
    
    Expected output
    0
    
  3. Example 3

    Input
    10 4
    200
    250
    300
    300
    350
    400
    500
    300
    250
    200
    3 1400
    2 500
    2 600
    1 900
    
    Expected output
    450
    
  4. Example 4

    Input
    5 3
    138
    583
    868
    822
    783
    2 523
    2 1015
    8 968
    
    Expected output
    2226
    
  5. Example 5

    Input
    8 4
    979
    884
    971
    870
    58
    94
    87
    370
    3 1508
    13 1372
    5 516
    10 435
    
    Expected output
    3878