#C11658. Minimum Total Cost

    ID: 40998 Type: Default 1000ms 256MiB

Minimum Total Cost

Minimum Total Cost

You are given two lists of non-negative integers: one represents the prices of items and the other represents the values of coupons. Each coupon can be used to reduce the price of one item. When a coupon with value c is applied to an item with price p, the new price becomes \(\max(0, p-c)\). Your task is to apply the coupons optimally in order to minimize the total cost of all items.

Note: You cannot apply a coupon to more than one item. If there are more items than coupons, items without a coupon are charged at full price. If there are extra coupons, they are simply unused.

Example:
For prices [100, 200, 300, 400] and coupons [50, 150, 100], one optimal way is to sort both lists in descending order and apply the largest coupon to the most expensive item. The minimal total cost calculated would be \(\max(0, 400-150) + \max(0, 300-100) + \max(0, 200-50) + 100 = 250 + 200 + 150 + 100 = 700\).

inputFormat

The input is read from stdin as follows:

  1. An integer n representing the number of items.
  2. n space-separated integers representing the prices of the items.
  3. An integer m representing the number of coupons.
  4. m space-separated integers representing the coupon values.

It is guaranteed that all numbers are non-negative integers.

outputFormat

Output a single integer to stdout which is the minimum total cost calculated after applying the coupons optimally. The calculation for each item is \(\max(0, p-c)\) where p is the price and c is the applied coupon value.

## sample
4
100 200 300 400
3
50 150 100
700