시간 제한메모리 제한제출정답맞힌 사람정답 비율
3 초 2048 MB31121140.741%

문제

JOI-kun lives in the country of IOI, which is famous for its large lake. Today, a stamp rally competition will be held around the lake.

Around the lake, there are $2N$ evenly spaced locations, numbered from $1$ to $2N$ in a clockwise manner. Additionally, there are $2N$ one-way roads connecting adjacent locations. Road $i$ ($1 ≤ i ≤ 2N - 1$) goes from location $i$ to location $i+1$, and road $2N$ goes from location $2N$ to location $1$. At the midpoint of each road, there is a stamp station.

There are $N$ colors of stamps numbered from $1$ to $N$. The color of the stamp that can be obtained at the stamp station on road $i$ ($1 ≤ i ≤ 2N$) is given by $A_i$. For each color $j$ ($1 ≤ j ≤ N$), there are exactly $2$ stamp stations where the stamp of that color can be obtained.

JOI-kun, equipped with many stamp cards, participates in the stamp rally competition. Each stamp card has two spaces, left and right, where stamps can be pressed. At most one stamp can be placed in each space. Initially, all stamp cards are blank.

The process of the stamp rally competition for JOI-kun is as follows:

  1. First, he selects one of the $2N$ locations as the starting point and moves there. If he selects location $i$ ($1 ≤ i ≤ 2N$), he must pay a participation cost of $C_i$.
  2. Next, he can instruct the event organizers to swap adjacent stamp stations. Specifically, he can swap the stamp stations on roads $2N$ and $1$, or swap the stations on roads $i - 1$ and $i$ for any $i$ ($2 ≤ i ≤ 2N$). Each swap costs $X$, and JOI-kun can issue as many swap commands as he wants, possibly none. Swaps are executed immediately upon command. However, to prevent cheating, it is not allowed to exchange stamp stations that cross the starting location that JOI-kun has chosen. That is, if he starts at location $1$, swapping the stations on roads $2N$ and $1$ is forbidden. If he starts at location $i$ ($2 ≤ i ≤ 2N$), swapping the stations on roads $i - 1$ and $i$ is forbidden.
  3. After that, JOI-kun starts from his chosen location and moves clockwise, visiting each of the $2N$ stamp stations in sequence. When visiting a stamp station, he can press the stamp onto his stamp cards as many times as he likes. He can also stamp both the left and right spaces of a single card at the same station. However, for each stamp card, he must always stamp the left space before the right space; that is, he cannot stamp the right space if the left space of that stamp card is still empty.

JOI-kun wants to collect many distinct types of stamp cards that are stamped on both spaces. Let stamped card $(a, b)$ be a stamp card with color $a$ stamped on the left and color $b$ stamped on the right. Two stamped cards $(a_1, b_1)$ and $(a_2, b_2)$ are considered the same type if and only if $a_1 = a_2$ and $b_1 = b_2$. Since there are $N$ colors of stamps, there are a total of $N^2$ possible types of stamped cards.

You need to answer $Q$ queries to help JOI-kun. The $q$-th query ($1 ≤ q ≤ Q$) asks the following:

  • What is the minimum total cost required for JOI-kun to collect at least $K_q$ types of stamped cards by the end of the rally? It is provable that under the given constraints, JOI-kun can always collect at least $K_q$ types of stamped cards by spending a sufficiently large cost.

Given the information about stamp colors, participation costs, swap costs, and queries, write a program to answer JOI-kun’s $Q$ queries.

입력

Read the following data from the standard input.

$N$ $X$

$A_1$ $A_2$ $\cdots$ $A_{2N}$

$C_1$ $C_2$ $\cdots$ $C_{2N}$

$Q$

$K_1$

$K_2$

$\vdots$

$K_Q$

출력

Write $Q$ lines to the standard output, where the $q$-th line ($1 ≤ q ≤ Q$) contains the minimum total cost required to collect at least $K_q$ types of stamped cards.

제한

  • $2 ≤ N ≤ 500\, 000$.
  • $1 ≤ X ≤ 500\, 000$.
  • $(A_1, A_2, \dots , A_{2N})$ is a permutation of $(1, 1, 2, 2, \dots , N, N)$.
  • $1 ≤ C_i ≤ 10^{18}$ ($1 ≤ i ≤ 2N$).
  • $1 ≤ Q ≤ 500\, 000$.
  • $1 ≤ K_q ≤ N^2$ ($1 ≤ q ≤ Q$).
  • Given values are all integers.

서브태스크

번호배점제한
15

$N ≤ 4$.

220

$N ≤ 5000$, $Q = 1$, $K_1 = N^2$.

320

$N ≤ 5000$, $Q = 1$.

419

$N ≤ 5000$.

521

$Q = 1$.

615

No additional constraints.

예제 입력 1

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

예제 출력 1

3
4

Consider the case where JOI-kun selects location $2$ as the starting point and instructs the exchange of the stamp stations installed on road $3$ and road $4$.

In this case,

  • The total cost paid by JOI-kun is $C_2 + X \times 1 = 3$.
  • JOI-kun visits the stamp stations in the order of roads $2, 3, 4, 5, 6, 1$, and the stamp colors available at each station in sequence are $2, 3, 2, 1, 3, 1$.
  • The number of types of stamp cards stamped on both spaces that JOI-kun can collect is $8$.
    • For example, to obtain a stamped card with a stamp of color 3 on the left and a stamp of color $1$ on the right, JOI-kun can stamp the left side at road $3$ and the right side at road $1$.
    • However, note that it is impossible to obtain a stamped card with a stamp of color $1$ on the left and color $2$ on the right.

Since it is impossible to obtain $8$ or more types of stamped cards with a cost of $2$ or less, the first line of the output should be $3$.

Additionally, if JOI-kun selects location $3$ as the starting point and does not instruct any stamp station exchange, he can obtain $9$ types of stamped cards.

In this case, the total cost paid by JOI-kun is $C_3 + X \times 0 = 4$. Since it is impossible to obtain $9$ or more types of stamped cards with a cost of $3$ or less, the second line of the output should be $4$.

This input example satisfies the constraints of subtasks 1, 4, 6.

예제 입력 2

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

예제 출력 2

7

Consider the case where JOI-kun chooses location $10$ as the starting point and instructs the stamp station exchanges in the following order:

  • Exchange the stamp station installed on road $15$ with the one on road $16$.
  • Exchange the stamp station installed on road $2$ with the one on road $3$.
  • Exchange the stamp station installed on road $16$ with the one on road $1$.
  • Exchange the stamp station installed on road $1$ with the one on road $2$.

In this case, JOI-kun can obtain $64$ types of stamped cards, and the total cost paid is $C_{10} + X \times 4 = 7$.

This input example satisfies the constraints of subtasks 2, 3, 4, 5, 6.

예제 입력 3

9 4
4 3 5 3 8 1 5 8 1 7 6 2 4 9 6 9 2 7
12 9 4 8 7 1 20 5 8 7 4 13 5 9 10 3 7 8
6
39
81
73
79
64
52

예제 출력 3

1
18
3
10
1
1

This input example satisfies the constraints of subtasks 4, 6.

채점 및 기타 정보

  • 예제는 채점하지 않는다.
  • 이 문제의 채점 우선 순위는 2이다.