AtCoder Beginner Contest 426
G
Range Knapsack Query
AtCoder
⌀ 2119
原题
翻译中…

Problem Statement

翻译中…

There are $N$ items numbered from $1$ to $N$. The weight of item $i$ is $W_i$ and the value is $V_i$.

翻译中…

You are given $Q$ queries, so process them in order. The $j$-th query is as follows:

翻译中…
  • Integers $L_j, R_j, C_j\ (1\leq L_j\leq R_j\leq N)$ are given. When choosing some (possibly zero) items from items $L_j,L_j+1,\dots,R_j$ such that the total weight does not exceed $C_j$, find the maximum possible total value of the chosen items.
翻译中…

Constraints

翻译中…
  • $1\leq N \leq 2\times 10^4$
  • $1\leq Q \leq 2\times 10^5$
  • $1\leq W_i \leq 500$
  • $1\leq V_i \leq 10^9$
  • $1\leq L_j \leq R_j \leq N$
  • $1\leq C_j \leq 500$
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

N
W_1 V_1
W_2 V_2
\vdots
W_N V_N
Q
L_1 R_1 C_1
L_2 R_2 C_2
\vdots
L_Q R_Q C_Q

翻译中…

Output

翻译中…

Output $Q$ lines. The $i$-th line $(1\leq i \leq Q)$ should contain the answer for the $i$-th query.

翻译中…

样例

Input #1
4
3 4
5 8
1 2
2 3
3
1 4 7
2 4 10
1 2 2
Output #1
11
13
0
For the first query, among items $1,2,3,4$, if you choose items $2,4$, the total weight is $5+2=7$, which does not exceed $C_1=7$, and the total value is $8+3=11$. This is the maximum. For the second query, even if you choose all items $2,3,4$, the total weight is $5+1+2=8$, which does not exceed $C_2=10$, and you can achieve a total value of $8+2+3=13$. For the third query, both items $1,2$ have weights exceeding $C_3=2$, so you cannot choose any item, and the maximum total value is $0$.
Input #2
8
167 430302156
22 623690081
197 476190629
176 24979445
22 877914575
247 211047202
232 822804784
25 628894325
8
6 8 176
3 5 80
1 7 310
4 8 368
4 5 218
3 4 431
4 6 228
1 1 239
Output #2
628894325
877914575
2324409440
2329613684
902894020
501170074
902894020
430302156
题目信息
题号
abc426_g
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。