luogu#P2308. 添加括号
添加括号
Background
Given a sequence of positive integers .
Without changing the position of each element in the sequence, add them together by inserting parentheses. Record the sum obtained at each addition step with parentheses; we call it an intermediate sum.
For example:
Given the sequence .
First way to add parentheses:
There are three intermediate sums , and their total is: .
Second way to add parentheses:
The intermediate sums are , and their total is .
Problem Description
Now we add pairs of parentheses. The additions are performed according to the parentheses order, producing intermediate sums. Find the way to add parentheses that minimizes the total of the intermediate sums.
Input Format
Two lines.
The first line contains an integer .
The second line contains positive integers , each not exceeding .
Output Format
Output 3 lines.
The first line is the parenthesization.
The second line is the final total of the intermediate sums.
The third line contains the intermediate sums, output from inner to outer and from left to right.
4
4 1 2 3
(4+((1+2)+3))
19
3 6 10
Hint
Sample Explanation
See the Background.
Constraints
For all testdata, .
Translated by ChatGPT 5