#P6787. 「SWTR-6」Snow Mountain
「SWTR-6」Snow Mountain
Description
There are some crystals in the cave. Each crystal has an energy value . The energy values can be large or small, but they will not be the same. These mysterious crystals are attached with souls of evil forces. Your task now is to destroy these crystals, and make the total evil energy they release as small as possible.
You may choose two undestroyed crystals , destroy them, and release evil energy, where indicates that this is the -th destruction.
However, there are some unordered crystal pairs . If you destroy them together, a strong resonance will occur and cause the cave to collapse, burying you inside!
With this piece of paper, Little A arrives at the end of the cave, and indeed finds crystals ( is even). As the paper says, each crystal has an energy value .
After observing these crystals, Little A discovers a rule: for each crystal , among all crystals whose energy values are greater than it, it will resonate with at most one of them; denote its index as .
Now Little A knows and . Can you help him find the minimum possible total evil energy released by destroying all crystals? If it is impossible to destroy them all, output . Otherwise, first output the minimum value, then output a destruction plan.
If there are multiple valid plans, output any one.
- Note that after destruction, the crystal indices do not change.
Simplified statement:
You are given two sequences of length , where all are distinct, and if , then .
Now you need to perform delete operations. In each operation, choose two undeleted numbers such that and , and delete these two numbers from sequence with cost (after deletion, the indices of the remaining elements do not change), where indicates that this is the -th deletion.
Find the minimum total cost and a plan to delete all numbers. If there is no solution, output . If there are multiple solutions, output any one.
Input Format
The first line contains an integer , and it is guaranteed that is even.
The second line contains integers , and it is guaranteed that all are distinct.
The next line contains integers . If , it means the -th crystal does not resonate with any crystal whose energy value is greater than it; otherwise it is guaranteed that .
The input size is large; using scanf is recommended.
Output Format
If there is no solution, output one line .
Otherwise, first output one line with an integer, meaning the minimum total evil energy released.
Then output lines, each containing two integers separated by a space, representing the pair of crystals destroyed in that operation.
The output size is large; using printf is recommended.
4
1 4 2 3
3 -1 -1 2
4
3 2
1 4
4
5 7 1 3
-1 -1 1 1
7
1 2
3 4
4
1 9 4 5
4 -1 4 2
-1
Hint
"Sample 3 Explanation"
It is impossible to destroy all crystals, because crystal cannot be destroyed.
"Constraints and Notes"
This problem uses bundled testdata.
- Subtask 1 (5 points): .
- Subtask 2 (20 points): .
- Subtask 3 (15 points): .
- Subtask 4 (20 points): .
- Subtask 5 (15 points): is in increasing order, i.e. .
- Subtask 6 (24 points): no special restrictions.
- Subtask 7 (1 point): hack testdata.
For of the testdata, , .
It is guaranteed that is even and all are distinct.
It is guaranteed that the answer does not exceed .
"Help / Tips"
Please pay attention to I/O optimization.
"Special Judge"
This problem uses SPJ.
Please read the output format carefully. Incorrect output format may cause UKE.
If the first line of your output is different from the first line of the official answer, you will get for this test point.
If there is no solution and the first line is the same, you will get for this test point.
If there is a solution and the first line is the same, but the plan is incorrect, you will get for this test point.
If there is a solution and the first line is the same, and the plan is correct, you will get for this test point.
A checker and testlib.h are also provided.
Baidu Netdisk link: link, extraction code: b7eg.
Translated by ChatGPT 5
京公网安备 11011102002149号