CF500F.New Year Shopping

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Dohyun is running a grocery store. He sells nn items numbered by integers from 1 to nn . The ii -th ( 1<=i<=n1<=i<=n ) of them costs cic_{i} dollars, and if I buy it, my happiness increases by hih_{i} . Each item can be displayed only for pp units of time because of freshness. As Dohyun displays the ii -th item at time tit_{i} , the customers can buy the ii -th item only from time tit_{i} to time ti+(p1)t_{i}+(p-1) inclusively. Also, each customer cannot buy the same item more than once.

I'd like to visit Dohyun's grocery store and buy some items for the New Year Party, and maximize my happiness. Because I am a really busy person, I can visit the store only once, and for very short period of time. In other words, if I visit the store at time tt , I can only buy the items available at time tt . But I can buy as many items as possible, if the budget holds. I can't buy same item several times due to store rules. It is not necessary to use the whole budget.

I made a list of qq pairs of integers (aj,bj)(a_{j},b_{j}) , which means I may visit the store at time aja_{j} , and spend at most bjb_{j} dollars at the store. For each pair, I'd like to know the maximum happiness I can obtain. But there are so many pairs that I can't handle them. Can you help me?

输入格式

The first line contains two space-separated integers nn and pp ( 1<=n<=40001<=n<=4000 , 1<=p<=100001<=p<=10000 ) — the number of items, and the display time of each item.

Next nn lines describe the items. The ii -th ( 1<=i<=n1<=i<=n ) of them contains three space-separated integers cic_{i} , hih_{i} , tit_{i} ( 1<=ci,hi<=40001<=c_{i},h_{i}<=4000 , 1<=ti<=100001<=t_{i}<=10000 ) — the cost of the ii -th item, the happiness of the ii -th item, and the time when the ii -th item starts to be displayed.

The next line contains an integer qq ( 1<=q<=200001<=q<=20000 )— the number of candidates.

Next qq lines describe the candidates. The jj -th ( 1<=j<=q1<=j<=q ) of them contains two space-separated integers aja_{j} , bjb_{j} ( 1<=aj<=200001<=a_{j}<=20000 , 1<=bj<=40001<=b_{j}<=4000 ) — the visit time and the budget for jj -th visit of store.

输出格式

For each candidate, print a single line containing the maximum happiness that I can obtain by buying some items.

输入输出样例

  • 输入#1

    4 4
    2 3 2
    3 5 1
    4 7 2
    11 15 5
    4
    1 3
    2 5
    2 6
    5 14
    

    输出#1

    5
    8
    10
    18
    
  • 输入#2

    5 4
    3 2 1
    7 4 4
    2 1 2
    6 3 5
    3 2 2
    10
    1 5
    2 5
    4 8
    4 9
    4 10
    5 8
    5 9
    5 10
    8 4
    7 9
    

    输出#2

    2
    3
    5
    5
    6
    4
    5
    6
    0
    4
    

说明/提示

Consider the first sample.

1. At time 1, only the 2nd item is available. I can buy the 2nd item using 3 dollars and my happiness will increase by 5.
2. At time 2, the 1st, 2nd, and 3rd item is available. I can buy the 1st item using 2 dollars, and the 2nd item using 3 dollars. My happiness will increase by 3 + 5 = 8.
3. At time 2, the 1st, 2nd, and 3rd item is available. I can buy the 1st item using 2 dollars, and the 3nd item using 4 dollars. My happiness will increase by 3 + 7 = 10.
4. At time 5, the 1st, 3rd, and 4th item is available. I can buy the 1st item using 2 dollars, and the 4th item using 11 dollars. My happiness will increase by 3 + 15 = 18. Note that I don't need to use the whole budget in this case.

首页