Collecting Stamps 4

시간 제한3초메모리 제한2048 MB

요약
출발 위치와 그 위치를 넘지 않는 인접 교환을 정할 때, 서로 다른 색 순서쌍을 K가지 이상 만들기 위한 최소 비용을 각 질의마다 구한다.
난이도

어려움10점 중 10점

유형
그리디, 정렬, 누적 합, 조합론
정답자
아직 제출이 없습니다

문제

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 2N2N evenly spaced locations, numbered from 11 to 2N2N in a clockwise manner. Additionally, there are 2N2N one-way roads connecting adjacent locations. Road ii (1≤i≤2N−11 ≤ i ≤ 2N - 1) goes from location ii to location i+1i+1, and road 2N2N goes from location 2N2N to location 11. At the midpoint of each road, there is a stamp station.

There are NN colors of stamps numbered from 11 to NN. The color of the stamp that can be obtained at the stamp station on road ii (1≤i≤2N1 ≤ i ≤ 2N) is given by A_iA\_i. For each color jj (1≤j≤N1 ≤ j ≤ N), there are exactly 22 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 2N2N locations as the starting point and moves there. If he selects location ii (1≤i≤2N1 ≤ i ≤ 2N), he must pay a participation cost of C_iC\_i.
  2. Next, he can instruct the event organizers to swap adjacent stamp stations. Specifically, he can swap the stamp stations on roads 2N2N and 11, or swap the stations on roads i−1i - 1 and ii for any ii (2≤i≤2N2 ≤ i ≤ 2N). Each swap costs XX, 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 11, swapping the stations on roads 2N2N and 11 is forbidden. If he starts at location ii (2≤i≤2N2 ≤ i ≤ 2N), swapping the stations on roads i−1i - 1 and ii is forbidden.
  3. After that, JOI-kun starts from his chosen location and moves clockwise, visiting each of the 2N2N 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)(a, b) be a stamp card with color aa stamped on the left and color bb stamped on the right. Two stamped cards (a_1,b_1)(a\_1, b\_1) and (a_2,b_2)(a\_2, b\_2) are considered the same type if and only if a_1=a_2a\_1 = a\_2 and b_1=b_2b\_1 = b\_2. Since there are NN colors of stamps, there are a total of N2N^2 possible types of stamped cards.

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

  • What is the minimum total cost required for JOI-kun to collect at least K_qK\_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_qK\_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 QQ queries.

입력

Read the following data from the standard input.

NN XX

A_1A\_1 A_2A\_2 ⋯\cdots A_2NA\_{2N}

C_1C\_1 C_2C\_2 ⋯\cdots C_2NC\_{2N}

QQ

K_1K\_1

K_2K\_2

⋮\vdots

K_QK\_Q

출력

Write QQ lines to the standard output, where the qq-th line (1≤q≤Q1 ≤ q ≤ Q) contains the minimum total cost required to collect at least K_qK\_q types of stamped cards.

제한

  • 2≤N≤500,0002 ≤ N ≤ 500\\, 000.
  • 1≤X≤500,0001 ≤ X ≤ 500\\, 000.
  • (A_1,A_2,…,A_2N)(A\_1, A\_2, \dots , A\_{2N}) is a permutation of (1,1,2,2,…,N,N)(1, 1, 2, 2, \dots , N, N).
  • 1≤C_i≤10181 ≤ C\_i ≤ 10^{18} (1≤i≤2N1 ≤ i ≤ 2N).
  • 1≤Q≤500,0001 ≤ Q ≤ 500\\, 000.
  • 1≤K_q≤N21 ≤ K\_q ≤ N^2 (1≤q≤Q1 ≤ q ≤ Q).
  • Given values are all integers.

예제3

  1. 예제 1

    입력
    3 2
    1 2 2 3 1 3
    6 1 4 5 4 7
    2
    8
    9
    
    예상 출력
    3
    4
    
  2. 예제 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
    
    예상 출력
    7
    
  3. 예제 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
    
    예상 출력
    1
    18
    3
    10
    1
    1