#P6787. 「SWTR-6」Snow Mountain

    ID: 5745 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>贪心2020Special JudgeO2优化构造洛谷月赛

「SWTR-6」Snow Mountain

Description

There are some crystals in the cave. Each crystal has an energy value aia_i. 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 i,ji,j, destroy them, and release min(ai,aj)×k\min(a_i,a_j)\times k evil energy, where kk indicates that this is the kk-th destruction.

However, there are some unordered crystal pairs (x,y)(x,y). 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 nn crystals (nn is even). As the paper says, each crystal has an energy value aia_i.

After observing these crystals, Little A discovers a rule: for each crystal ii, among all crystals whose energy values are greater than it, it will resonate with at most one of them; denote its index as xix_i.

Now Little A knows aia_i and xix_i. 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 -1\texttt{-1}. 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 a,xa,x of length n (2n)n\ (2|n), where all aia_i are distinct, and if xi1x_i \neq -1, then axi>aia_{x_i}>a_i.

Now you need to perform n2\frac{n}{2} delete operations. In each operation, choose two undeleted numbers ai,aja_i,a_j such that xijx_i\neq j and xjix_j\neq i, and delete these two numbers from sequence aa with cost min(ai,aj)×k\min(a_i,a_j)\times k (after deletion, the indices of the remaining elements do not change), where kk indicates that this is the kk-th deletion.

Find the minimum total cost and a plan to delete all numbers. If there is no solution, output -1\texttt{-1}. If there are multiple solutions, output any one.

Input Format

The first line contains an integer nn, and it is guaranteed that nn is even.

The second line contains nn integers a1,a2,,ana_1,a_2,\cdots,a_n, and it is guaranteed that all aia_i are distinct.

The next line contains nn integers xix_i. If xi=1x_i=-1, it means the ii-th crystal does not resonate with any crystal whose energy value is greater than it; otherwise it is guaranteed that axi>aia_{x_i}>a_i.

The input size is large; using scanf is recommended.

Output Format

If there is no solution, output one line -1\texttt{-1}.

Otherwise, first output one line with an integer, meaning the minimum total evil energy released.

Then output n2\dfrac{n}{2} 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 44 cannot be destroyed.

"Constraints and Notes"

This problem uses bundled testdata.

  • Subtask 1 (5 points): n=2n=2.
  • Subtask 2 (20 points): n10n \leq 10.
  • Subtask 3 (15 points): xi=1x_i=-1.
  • Subtask 4 (20 points): n3×103n\leq 3\times 10^3.
  • Subtask 5 (15 points): aia_i is in increasing order, i.e. ai<ai+1 (1i<n)a_i<a_{i+1}\ (1\leq i<n).
  • Subtask 6 (24 points): no special restrictions.
  • Subtask 7 (1 point): hack testdata.

For 100%100\% of the testdata, 2n5×1052 \leq n \leq 5 \times 10^5, 1ai1091 \leq a_i \leq 10^9.
It is guaranteed that nn is even and all aia_i are distinct.
It is guaranteed that the answer does not exceed 26312^{63}-1.

"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 0%0\% for this test point.

If there is no solution and the first line is the same, you will get 100%100\% for this test point.

If there is a solution and the first line is the same, but the plan is incorrect, you will get 60%60\% for this test point.

If there is a solution and the first line is the same, and the plan is correct, you will get 100%100\% for this test point.

A checker and testlib.h are also provided.

Baidu Netdisk link: link, extraction code: b7eg.

Translated by ChatGPT 5