PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CT0000132. Dãy xấp xỉ tăng (Increasing Sequence Approximation)

    ID: 1639 Loại: Thông thường 1000ms 256MiB Tried: 0 Đã chấp nhận: 0 Độ khó: 7 Đăng bởi: Nhãn>Greedy AlgorithmsGreedy proof techniquesData StructuresPriority queueDynamic ProgrammingDynamic programming basics

Bài tập này chưa được xếp vào lộ trình.

Dãy xấp xỉ tăng (Increasing Sequence Approximation)

Increasing Sequence Approximation

Source: Bộ Giáo dục và Đào tạo

Version: Phuoc Hung OJ Extended

Problem Statement

Given an integer sequence A=(a1,a2,…,aN)A=(a_1,a_2,\ldots,a_N), construct an integer sequence B=(b1,b2,…,bN)B=(b_1,b_2,\ldots,b_N) that is strictly increasing, meaning b1<b2<⋯<bNb_1<b_2<\cdots<b_N, and minimizes the sum of absolute deviations

d(A,B)=∑i=1N∣ai−bi∣.d(A,B)=\sum_{i=1}^{N}|a_i-b_i|.

The values bib_i may be negative even though the input values are nonnegative.

If several sequences BB achieve the same minimum sum, any one of them may be output. Print both the minimum sum and the corresponding sequence BB.

Input

  • The first line contains a positive integer NN.
  • The second line contains NN integers a1,a2,…,aNa_1,a_2,\ldots,a_N.

Output

  • The first line prints the minimum value d(A,B)d(A,B).
  • The second line prints exactly NN strictly increasing integers b1,b2,…,bNb_1,b_2,\ldots,b_N achieving that minimum. Any optimal sequence is accepted.

Subtasks

  • Subtask 1 (20 points): N=3N=3; 0≤ai≤1090\le a_i\le10^9 for all 1≤i≤N1\le i\le N.
  • Subtask 2 (20 points): 1≤N≤3001\le N\le300; 0≤ai≤3000\le a_i\le300 for all 1≤i≤N1\le i\le N.
  • Subtask 3 (20 points): 1≤N≤3001\le N\le300; 0≤ai≤1090\le a_i\le10^9 for all 1≤i≤N1\le i\le N.
  • Subtask 4 (20 points): 1≤N≤30001\le N\le3000; 0≤ai≤1090\le a_i\le10^9 for all 1≤i≤N1\le i\le N.
  • Subtask 5 (20 points): 1≤N≤3000001\le N\le300000; 0≤ai≤1090\le a_i\le10^9 for all 1≤i≤N1\le i\le N.

For each test, 50% of its score is awarded for the correct minimum deviation, and the remaining 50% is awarded for a valid optimal sequence BB.

Examples

Example 1

Input

7
1 5 1 7 3 1 3

Output

17
-1 0 1 2 3 4 5

Explanation: The output sequence (−1,0,1,2,3,4,5)(-1,0,1,2,3,4,5) is strictly increasing. Its total deviation is $|1-(-1)|+|5-0|+|1-1|+|7-2|+|3-3|+|1-4|+|3-5|=2+5+0+5+0+3+2=17$, the minimum possible value.