This page is still under construction.

Parts of this page are still being built. What you see may change.

WTF Transformation

Time limit1sMemory limit256 MB

Summary
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 AA of NN integers, indexed from A[1]A[1] to A[N]A[N], and an integer RR.

A second array IDID holds N+1N + 1 integers, indexed from ID[1]ID[1] to ID[N+1]ID[N+1], and every one of them lies in the interval [1,N−1][1, N-1].

The Warshall-Turing-Fourier transformation of AA under IDID 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 AA to the right by RR places moves the element at position jj to position ((j+R−1) mod N)+1((j + R - 1) \bmod N) + 1. 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 IDID is at most N−1N - 1, so the index max⁡(ID[i],ID[i+1])+1\max(ID[i], ID[i+1]) + 1 never leaves the interval [1,N][1, N].

You know AA and RR, but not IDID. Find the largest value of sum that a choice of IDID can produce.

Input

The first line contains the integers NN and RR (2≤N≤30002 \le N \le 3000, 1≤R<N1 \le R < N).

The second line contains NN integers, A[1]A[1] to A[N]A[N], each from the interval [−104,104][-10^4, 10^4].

Output

On the first line, print the largest value of sum.

On the second line, print the N+1N + 1 integers ID[1]ID[1] to ID[N+1]ID[N+1] 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 ID[1]ID[1]; if several remain, take the one with the smallest ID[2]ID[2], and continue in the same way.

Examples2

  1. Example 1

    Input
    5 3
    1 -1 1 -1 1
    
    Expected output
    10
    1 1 1 2 2 3
    
  2. Example 2

    Input
    6 5
    2 5 4 1 3 5
    
    Expected output
    16
    3 2 1 1 5 4 1