#P9344. 去年天气旧亭台

    ID: 9968 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>动态规划 DP贪心2023洛谷原创O2优化洛谷月赛

去年天气旧亭台

Background

It is still the weather of the past, and the misty rain over the terraces of the past. Time quietly flows away; the mountains and rivers remain, but the people are no longer the same as before.

Problem Description

Climbing up to the terrace, the old floor covered with dust comes into view.

There are nn floorboards in total. The floorboards are of two types: the type of the ii-th floorboard is denoted by cic_i, and its dust level is denoted by aia_i. Note that cic_i is either 00 or 11.

Now you need to clean the dust on these floorboards. In each operation, you may:

  • Choose two indices i,ji, j satisfying 1ijn1 \le i \le j \le n, ci=cjc_i = c_j, and the dust on both the ii-th and the jj-th floorboards has not been cleaned yet;
  • Spend ai+aja_i + a_j energy to clean the dust on all floorboards from the ii-th to the jj-th.

Find the minimum energy required to clean all floorboards.

Input Format

This problem has multiple test cases.

The first line contains an integer TT, the number of test cases.

For each test case:

  • The first line contains an integer nn.
  • The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n.
  • The third line contains nn integers c1,c2,,cnc_1, c_2, \dots, c_n.

Output Format

For each test case, output one integer per line, the minimum energy.

2
6
1 1 4 5 1 4
1 0 0 1 0 1
8
3 1 4 1 5 9 2 6
1 0 1 0 1 0 1 0
5
13

Hint

[Sample 1 Explanation]

  • For the first test case, directly spend a1+a6=5a_1 + a_6 = 5 energy to clean all the dust.
  • For the second test case, first spend a1+a1=6a_1 + a_1 = 6 energy to clean the dust on the first floorboard, then spend a2+a8=7a_2 + a_8 = 7 energy to clean the remaining dust.

[Constraints]

For 10%10\% of the testdata, it is guaranteed that T10T \le 10 and n10n \le 10.

For 40%40\% of the testdata, it is guaranteed that T20T \le 20 and n103n \le 10^3.

For another 10%10\% of the testdata, it is guaranteed that ci=1c_i = 1.

For 100%100\% of the testdata, it is guaranteed that 1T1051 \le T \le 10^5, 1n,n2×1061 \le n, \sum n \le 2 \times 10^6, ci{0,1}c_i \in \{0,1\}, and 1ai1091 \le a_i \le 10^9.

Translated by ChatGPT 5