#P9744. 「KDOI-06-S」消除序列

    ID: 9987 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>递推2023洛谷原创O2优化前缀和洛谷月赛

「KDOI-06-S」消除序列

Problem Description

Little M has a sequence v1,v2,,vnv_1, v_2, \ldots, v_n of length nn. Initially, all elements are 11.

You have 33 types of operations on this sequence:

  1. Choose an index ii (1in1 \le i \le n), and set v1,v2,,viv_1, v_2, \ldots, v_i all to 00. The cost of this operation is aia_i.
  2. Choose an index ii (1in1 \le i \le n), and set viv_i to 00. The cost of this operation is bib_i.
  3. Choose an index ii (1in1 \le i \le n), and set viv_i to 11. The cost of this operation is cic_i.

Now there are qq queries. In each query, you are given a set PP. You want to perform some operations (possibly 00 operations) so that: the elements whose indices belong to this set have value 11, and all other positions have value 00. Formally, for any 1in\bm{1 \le i \le n}, if iP\bm{i \in P} then vi=1\bm{v_i = 1}, otherwise vi=0\bm{v_i = 0}. You also need to minimize the total cost of all operations in this query.

Note that the queries are independent. That is, after each query ends, the sequence vv returns to the initial state, where all elements become 11 again.

Input Format

Read input from standard input.

The first line contains a positive integer nn, denoting the length of the sequence vv.

The second line contains nn non-negative integers a1,a2,,ana_1, a_2, \ldots, a_n, denoting the cost of the first type of operation.

The third line contains nn non-negative integers b1,b2,,bnb_1, b_2, \ldots, b_n, denoting the cost of the second type of operation.

The fourth line contains nn non-negative integers c1,c2,,cnc_1, c_2, \ldots, c_n, denoting the cost of the third type of operation.

The fifth line contains a positive integer qq, denoting the number of queries.

In the following qq lines, the ii-th line contains:

  • A leading non-negative integer mm, denoting the size of set PP in the ii-th query.
  • Then mm positive integers p1,p2,,pmp_1, p_2, \ldots, p_m, in order, denoting each element in set PP. It is guaranteed that for any 1i<m1 \le i < m, pi<pi+1p_i < p_{i+1}.

Output Format

Write to standard output.

Output qq lines. The ii-th line contains a non-negative integer, denoting the minimum total cost of operations in the ii-th query.

5
1 13 6 0 6
2 4 1 0 5
3 4 1 2 1
7
1 4
2 1 5
1 4
2 2 3
5 1 2 3 4 5
1 5
2 3 4
7
3
7
6
0
0
8
7
10 1 6 9 4 2 4 
0 5 2 3 0 1 4 
4 1 4 1 5 3 5 
6
3 1 3 6 
2 2 6 
4 3 4 5 7 
1 4 
2 3 7 
3 3 5 6
12
8
2
5
5
8
10
6 10 7 2 8 4 6 4 8 7
4 0 6 7 8 4 8 2 10 5
4 10 6 1 4 7 5 3 8 7
1
0
7

Hint

[Sample Explanation #1]

For the first query, you can perform the following operations in order:

  • Perform operation 11 at i=4i = 4. After that, the sequence vv becomes [0,0,0,0,1][0, 0, 0, 0, 1], with cost 00.
  • Perform operation 33 at i=4i = 4. After that, the sequence vv becomes [0,0,0,1,1][0, 0, 0, 1, 1], with cost 22.
  • Perform operation 22 at i=5i = 5. After that, the sequence vv becomes [0,0,0,1,0][0, 0, 0, 1, 0], with cost 55.

So the total cost is 0+2+5=70 + 2 + 5 = 7. It can be proven that there is no smaller total cost.

[Sample Explanation #3]

For the only query in this sample, you can choose to perform operation 11 at i=10i = 10, with total cost a10=7a_{10} = 7.

[Sample #4]

See reserve/reserve4.in and reserve/reserve4.ans under the contestant directory.

[Sample #5]

See reserve/reserve5.in and reserve/reserve5.ans under the contestant directory.

This sample satisfies the constraints of test points 8118 \sim 11.

[Sample #6]

See reserve/reserve6.in and reserve/reserve6.ans under the contestant directory.

This sample satisfies the constraints of test points 141514 \sim 15.

[Sample #7]

See reserve/reserve7.in and reserve/reserve7.ans under the contestant directory.

This sample satisfies the constraints of test point 1616.

[Sample #8]

See reserve/reserve8.in and reserve/reserve8.ans under the contestant directory.

This sample satisfies the constraints of test points 172017 \sim 20.


[Constraints]

Let m\sum m be the sum of all mm over all queries within one test case.

It is guaranteed for all data that: 1n5×1051 \leq n \leq 5 \times 10^5, 0mn0 \le m \le n, 0m5×1050 \leq \sum m \leq 5 \times 10^5, 1qmax(n,m)1 \le q \le \max(n, \sum m), 0ai,bi,ci1090 \le a_i, b_i, c_i \le 10^9, 1pin1 \le p_i \le n.

Test Point ID nn \le mm \le m\sum m \le Special Property
121 \sim 2 5×1055 \times 10^5 00 No
343 \sim 4 77 1515
565 \sim 6 2×1032 \times 10^3 11 2×1032 \times 10^3
77 2×1032 \times 10^3 Yes
8118 \sim 11 No
121312 \sim 13 5×1045 \times 10^4
141514 \sim 15 5×1055 \times 10^5 11 5×1055 \times 10^5
1616 5×1055 \times 10^5 Yes
172017 \sim 20 No

Special property: for any 1in1 \le i \le n, it is guaranteed that ci=0c_i = 0.

[Hint]

The input and output sizes of this problem are large, so please use an appropriate I/O method.

Translated by ChatGPT 5