#P9742. 「KDOI-06-J」贡献系统

    ID: 10854 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>贪心2023洛谷原创O2优化洛谷月赛

「KDOI-06-J」贡献系统

Problem Description

The Luogu contribution system is online!

Now there are nn people who are going to participate in a Luogu monthly contest, and everyone’s rating is distinct. The ii-th person’s rating is rir_i, and their contribution value is cic_i.

Suppose the ii-th person’s rating rank among these nn people is aia_i (ranking by rating from high to low), and their rank in the monthly contest is bib_i. No two people have the same rank. That is, both aa and bb are permutations of 11 to nn. After the contest ends, everyone will perform the following operation:

  • If ai=bia_i=b_i, then the ii-th person’s rating does not change, so the ii-th person does nothing.
  • If ai>bia_i>b_i, then the ii-th person’s rating increases, so the ii-th person will upvote the problem setter, causing the problem setter’s contribution value to increase by cic_i (cic_i may be negative, in which case the problem setter’s contribution value decreases).
  • If ai<bia_i<b_i, then the ii-th person’s rating decreases, so the ii-th person will downvote the problem setter, causing the problem setter’s contribution value to decrease by cic_i (cic_i may be negative, in which case the problem setter’s contribution value increases).

As the only problem setter of this monthly contest, your initial contribution value is 00. You want to know: among all possible permutations bb (obviously, the permutation aa has already been determined before the contest), what is the maximum possible contribution value you can have after the contest ends.

Input Format

Read input from standard input.

This problem has multiple test cases.

The first line contains a positive integer TT, indicating the number of test cases.

For each test case, the first line contains a positive integer nn, indicating the number of contestants.

The second line contains nn non-negative integers r1,r2,,rnr_1,r_2,\ldots,r_n, indicating the contestants’ ratings. It is guaranteed that for any 1i<n1\le i< n, ri>ri+1r_i>r_{i+1}.

The third line contains nn integers c1,c2,,cnc_1,c_2,\ldots,c_n, indicating the contestants’ contribution values.

Output Format

Output to standard output.

For each test case, output one line with one integer, representing the maximum contribution value.

3
5
3816 3738 3726 3621 3582
111 109 -50 -22 208
8
8 7 6 5 4 3 2 1
128 1 0 0 0 0 1 0
10
10 9 8 7 6 5 4 3 2 1
1 1 4 5 1 4 1 9 1 9

280
1
34

Hint

[Sample Explanation #1]

For the first test case, label the five people in input order as A, B, C, D, E. Then when the contest ranking order is ABECD, the contribution value is maximized, equal to 0+0(50)(22)+208=2800+0-(-50)-(-22)+208=280. It can be proven that this is the only ranking order that can make the contribution value reach the maximum.

For the second test case, label the eight people in input order as A, B, C, D, E, F, G, H. Then when the contest ranking order is ABCDEGFH, the contribution value can reach the maximum value 11. Note that in this case, there are multiple ranking orders that achieve the maximum contribution value.

[Sample #2]

See contrib/contrib2.in and contrib/contrib2.ans in the contestant directory.

[Sample #3]

See contrib/contrib3.in and contrib/contrib3.ans in the contestant directory.


[Constraints]

For all data, it is guaranteed that: 1T51\le T\le 5, 1n2×1051\le n\le 2\times 10^5, 0ri1090\le r_i\le 10^9, 109ci109-10^9\le c_i\le 10^9, and for any 1i<n1\le i<n, ri>ri+1r_i>r_{i+1}.

Test Point ID nn\le Special Constraints
131\sim3 88 None
44 100100 ABC
55 C
676\sim 7 None
898\sim 9 5×1035\times 10^3 AB
101110\sim 11 C
121412\sim 14 None
1515 2×1052\times10^5 AB
161816\sim 18 B
192119\sim 21 C
222522\sim 25 2×1052\times 10^5 None
  • Special Property A: For any 1i<n1\le i<n, it is guaranteed that ci=ci+1c_i=c_{i+1}.
  • Special Property B: For any 1i<n1\le i<n, it is guaranteed that cici+1c_i\le c_{i+1}.
  • Special Property C: For any 1in1\le i\le n, it is guaranteed that ci0c_i\ge 0.

Translated by ChatGPT 5