#P11520. Minimizing Bicycle Ride Expenses with Ride Cards

    ID: 13608 Type: Default 1000ms 256MiB

Minimizing Bicycle Ride Expenses with Ride Cards

Minimizing Bicycle Ride Expenses with Ride Cards

During a long summer holiday of n days, Xiao Rei plans to ride a shared bicycle each day for si minutes, with a cost of c per minute. To save money, she can buy ride cards from the APP. There are m types of ride cards available. The i-th ride card has:

  • Price wi (in yuan),
  • Validity period di days (starting from the day of purchase),
  • Free riding time per day: ti minutes within the validity period.

Xiao Rei can purchase any ride card any number of times, and can even hold multiple cards whose valid periods overlap. On any day, if several ride cards are active, the free riding time for that day is \( \max\{t_i\} \) (i.e. the maximum among all active cards). For any riding time beyond the available free minutes that day, she must pay c per minute.

Help Xiao Rei to minimize her total expenditure during the holiday by planning the optimal purchase strategy.

The input format is as follows:

n c
s1 s2 ... sn
m
w1 d1 t1
...
wm dm tm

The output is a single integer representing the minimum total cost.

inputFormat

The first line contains two integers n and c — the number of days in the holiday and the cost per minute of riding.

The second line contains n integers s1, s2, ..., sn where si represents the riding time (in minutes) on the i-th day.

The third line contains a single integer m — the number of types of ride cards available.

Each of the next m lines contains three integers wi, di, ti describing a ride card.

outputFormat

Output a single integer which is the minimum total amount Xiao Rei needs to spend for her holiday rides.

sample

3 2
3 4 5
1
5 2 3
16