luogu#P5078. Tweetuzki 爱军训

Tweetuzki 爱军训

Problem Description

Tweetuzki still remembers some things about the instructor of their class during the military training in Grade 7.

There are nn students in Tweetuzki’s class, with seat numbers from 11 to nn. Once, the instructor ordered the nn students to stand in a line from left to right in increasing seat number, where student 11 is on the far left and student nn is on the far right.

Since the students had been standing for a long time and complained a lot, the kind instructor also did not want everyone to be dismissed at the same time and cause chaos, so he came up with a method: the instructor starts from the far left (next to student 11), walks to the right side of student nn, then turns back and returns to student 11. During the trip from student 11 to student nn, when the instructor reaches a student, he may choose to dismiss this student from the line, or wait until the return trip to dismiss the student.

However, some students behaved very badly during the training, so the instructor wants them to rest later. For student ii, define their “badness value” as wiw_i. The instructor wants, within one round trip, to have all students dismissed, and maximize the value of i=1nri×wi\sum_{i = 1}^n r_i \times w_i, where rir_i means student ii is the rir_i-th student to be dismissed. The clever instructor immediately found a plan, and Tweetuzki was amazed, so he asked the instructor how to do it. But he is timid and has a large “badness value”, so the instructor might not tell him, and thus he came to you.

Input Format

The first line contains an integer nn ( 5n1065 \le n \le 10^6 ).
The second line contains nn integers describing the badness values w1,w2,w3,,wnw_1, w_2, w_3, …, w_n ( 0wi1060 \le w_i \le 10^6 ).

Output Format

The first line contains an integer, the maximum value.
The next line contains nn integers describing a valid dismissal order. Output the corresponding students’ “badness values”; see the sample for details. If there are multiple valid dismissal orders, output the one with the lexicographically smallest sequence of dismissed student indices.

5
7 8 1 2 5
87
1 2 5 8 7
5
7 99 1 5 2
530
7 1 2 5 99
8
1 9 2 6 0 8 1 7
206
1 2 0 1 7 8 6 9

Hint

Sample Explanation 1

On the way there, dismiss students 3,4,53, 4, 5. On the way back, dismiss students 2,12, 1. The total value is $5 \times 7 + 4 \times 8 + 1 \times 1 + 2 \times 2 + 3 \times 5 = 87$, and it can be proven that no plan can achieve a value greater than 8787.

Constraints

This problem has 2020 test points, and each group of testdata is worth 55 points.

For 10%10\% of the data, n8n \le 8.
For 30%30\% of the data, n20n \le 20.
For 60%60\% of the data, n1000n \le 1000.
For 100%100\% of the data, 5n1065 \le n \le 10^6, 0wi1060 \le w_i \le 10^6.

Translated by ChatGPT 5