#P1040. [NOIP 2003 提高组] 加分二叉树
[NOIP 2003 提高组] 加分二叉树
Description
Let a binary tree with nodes have an inorder traversal of , where are the node indices. Each node has a positive integer score; denote the score of node as . The tree and each of its subtrees have a score. For any subtree (including itself), its score is computed as follows:
score of the left subtree of score of the right subtree of score of the root of .
If a subtree is empty, its score is defined as . The score of a leaf equals the score of the leaf node itself; its empty children are not considered.
Find a binary tree whose inorder traversal is and whose score is maximized. Output:
- The maximum score of .
- The preorder traversal of .
Input Format
The first line contains one integer , the number of nodes.
The second line contains space-separated integers, the score of each node.
Output Format
The first line contains one integer, the maximum score ().
The second line contains space-separated integers, the preorder traversal of the tree.
5
5 7 1 2 10
145
3 1 2 4 5
Hint
Constraints
For all testdata, it is guaranteed that , each node’s score is a positive integer less than , and the answer does not exceed .
Translated by ChatGPT 5
京公网安备 11011102002149号