#P8927. Maximizing the Rainmaking Walk Distance
Maximizing the Rainmaking Walk Distance
Maximizing the Rainmaking Walk Distance
In order to prevent the shrine from being bombed, Reimu needs to make it rain. She intends to invoke the rain by constructing magical arrays (or "法阵") on a straight road. There are n positions, and at each given position a magical array is built. The positions are provided in an array \(a = [a_1, a_2, \dots, a_n]\). Your task is to assign labels to these magical arrays (i.e. produce a permutation of the indices \(1,2,\dots,n\)) so that the effect of Reimu's walk is maximized.
Reimu will verify the effect by taking a cyclic walk. She starts at the magical array labeled 1, then proceeds to the one labeled 2, and so on until the array labeled \(n\), and finally returns from the array \(n\) back to the array labeled 1.
Because of the special properties of the magical arrays, the distance between the array at position \(a_i\) (with label \(i\)) and the array at position \(a_{i+1}\) (with label \(i+1\)) is given by:
\(\left|a_i \times p - a_{i+1} \times q\right|\)
Similarly, the distance from the \(n\)th array back to the 1st array is:
\(\left|a_n \times p - a_1 \times q\right|\)
Here, \(p\) and \(q\) are two given constants. The goal is to choose a permutation of the indices such that the total walking distance (the sum of the above distances for all consecutive pairs in the cycle) is maximized. If there are multiple answers, you may output any one of them.
The output should also include the maximum total walking distance.
inputFormat
The first line contains three integers \(n\), \(p\) and \(q\) separated by spaces.
The second line contains \(n\) integers representing the array \(a\): \(a_1, a_2, \dots, a_n\).
outputFormat
Output two lines:
The first line contains the maximum total walking distance.
The second line contains a permutation of the numbers from 1 to \(n\) (space-separated) that achieves this maximum distance.
sample
3 2 1
1 2 3
8
1 3 2
</p>