Riding Roller Coasters

Time limit1sMemory limit128 MB

Summary
Given N coasters with fun a_i-(k-1)^2*b_i per ride and fixed ride times, answer Q queries for the maximum total fun within each time budget.
Level

Medium7 of 10

Topics
Dynamic programming, Greedy, Prefix sum, Implementation
Solved
No attempts yet

Problem

Sang-geun and his friends went to an amusement park. The park has many kinds of roller coasters, and Sang-geun has analyzed each one in advance, writing down the fun he feels from riding it as a number. However, the more times he rides the same roller coaster, the less fun it becomes.

Sang-geun defined the fun of riding roller coaster ii for the kk-th time as the following function.

f(i,k)=ai−(k−1)2⋅bif(i, k) = a_i - (k-1)^2 \cdot b_i

If f(i,k)f(i, k) is not positive, riding that roller coaster again gives him no fun at all.

Sang-geun wants to ride roller coasters so that the total fun is maximized. Given the amount of time he can stay in the park, find the maximum total fun he can obtain while the total time spent riding roller coasters does not exceed that time. Riding roller coaster ii once takes a fixed amount of time.

Input

The first line contains the number of roller coasters NN. (0<N≤1000 < N \le 100)

Each of the next NN lines contains three integers aia_i, bib_i, and tit_i. Here aia_i and bib_i are the coefficients of the fun function, and tit_i is the time it takes to ride roller coaster ii once. (0≤ai,bi≤1,0000 \le a_i, b_i \le 1{,}000, 0<ti≤25,0000 < t_i \le 25{,}000)

The next line contains the number of park visits QQ. (0≤Q≤1,0000 \le Q \le 1{,}000)

Each of the next QQ lines contains a time TiT_i that Sang-geun can stay in the park. (0≤Ti≤25,0000 \le T_i \le 25{,}000)

Output

Print QQ lines. For each visit time TiT_i, print on one line the maximum total fun Sang-geun can obtain while the total time spent riding roller coasters does not exceed TiT_i.

Examples2

  1. Example 1

    Input
    2
    5 0 5
    7 0 7
    4
    88
    5
    6
    7
    
    Expected output
    88
    5
    5
    7
    
  2. Example 2

    Input
    1
    100 3 2
    5
    2
    3
    4
    5
    100
    
    Expected output
    100
    100
    197
    197
    435