Nile
문제
You want to transport artifacts through the Nile. The artifacts are numbered from to . The weight of artifact (0 ≤ i < N) is .
To transport the artifacts, you use specialized boats. Each boat can carry at most two artifacts.
- If you decide to put a single artifact in a boat, the artifact weight can be arbitrary.
- If you want to put two artifacts in the same boat, you have to make sure the boat is balanced evenly. Specifically, you can send artifacts and (0 ≤ p < q < N) in the same boat only if the absolute difference between their weights is at most , that is |W[p] - W[q]| ≤ D.
To transport an artifact, you have to pay a cost that depends on the number of artifacts carried in the same boat. The cost of transporting artifact (0 ≤ i < N) is:
- , if you put the artifact in its own boat, or
- , if you put it in a boat together with some other artifact.
Note that in the latter case, you have to pay for both artifacts in the boat. Specifically, if you decide to send artifacts and (0 ≤ p < q < N) in the same boat, you need to pay .
Sending an artifact in a boat by itself is always more expensive than sending it with some other artifact sharing the boat with it, so for all such that 0 ≤ i < N.
Unfortunately, the river is very unpredictable and the value of changes often. Your task is to answer questions numbered from to . The questions are described by an array of length . The answer to question (0 ≤ j < Q) is the minimum total cost of transporting all artifacts, when the value of is equal to .