WTF Transformation
Time limit1sMemory limit256 MB
Choose the ID array that maximizes the two-phase rotating sum and output that maximum with the lexicographically smallest optimal ID array.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Prefix sum, Math
- Solved
- No attempts yet
Problem
You are given an array of integers, indexed from to , and an integer .
A second array holds integers, indexed from to , and every one of them lies in the interval .
The Warshall-Turing-Fourier transformation of under is the following algorithm. (The transformation is made up. It does not exist outside this problem.)
sum = 0
for i = 1 to N
index = min(ID[i], ID[i+1])
sum = sum + A[index]
rotate A to the right by R places
negate every element of A
for i = 1 to N
index = max(ID[i], ID[i+1]) + 1
sum = sum + A[index]
rotate A to the right by R places
Rotating to the right by places moves the element at position to position . Both loops read and rotate the same array, so each rotation carries into the next step, and the sign change applies to the array as it stands after the first loop.
Every value of is at most , so the index never leaves the interval .
You know and , but not . Find the largest value of sum that a choice of can produce.
Input
The first line contains the integers and (, ).
The second line contains integers, to , each from the interval .
Output
On the first line, print the largest value of sum.
On the second line, print the integers to that reach this value, separated by single spaces. Several arrays may reach it, so print the lexicographically smallest one: among the arrays that reach the maximum, take the one with the smallest ; if several remain, take the one with the smallest , and continue in the same way.