#P9742. 「KDOI-06-J」贡献系统
「KDOI-06-J」贡献系统
Problem Description
The Luogu contribution system is online!
Now there are people who are going to participate in a Luogu monthly contest, and everyone’s rating is distinct. The -th person’s rating is , and their contribution value is .
Suppose the -th person’s rating rank among these people is (ranking by rating from high to low), and their rank in the monthly contest is . No two people have the same rank. That is, both and are permutations of to . After the contest ends, everyone will perform the following operation:
- If , then the -th person’s rating does not change, so the -th person does nothing.
- If , then the -th person’s rating increases, so the -th person will upvote the problem setter, causing the problem setter’s contribution value to increase by ( may be negative, in which case the problem setter’s contribution value decreases).
- If , then the -th person’s rating decreases, so the -th person will downvote the problem setter, causing the problem setter’s contribution value to decrease by ( 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 . You want to know: among all possible permutations (obviously, the permutation 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 , indicating the number of test cases.
For each test case, the first line contains a positive integer , indicating the number of contestants.
The second line contains non-negative integers , indicating the contestants’ ratings. It is guaranteed that for any , .
The third line contains integers , 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 . 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 . 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: , , , , and for any , .
| Test Point ID | Special Constraints | |
|---|---|---|
| None | ||
| ABC | ||
| C | ||
| None | ||
| AB | ||
| C | ||
| None | ||
| AB | ||
| B | ||
| C | ||
| None |
- Special Property A: For any , it is guaranteed that .
- Special Property B: For any , it is guaranteed that .
- Special Property C: For any , it is guaranteed that .
Translated by ChatGPT 5