#P4331. [BalticOI 2004] Sequence (Day1)

    ID: 3252 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2004线段树Special Judge左偏树BalticOI

[BalticOI 2004] Sequence (Day1)

Description

Given a sequence t1,t2,,tnt_1, t_2, \dots, t_n, find an increasing sequence z1,z2,,znz_1, z_2, \dots, z_n such that the sum of absolute differences t1z1+t2z2++tnzn|t_1 - z_1| + |t_2 - z_2| + \dots + |t_n - z_n| is minimized.

Input Format

The first line contains an integer nn. The next nn lines each contain an integer, representing the given sequence values tit_i.

Output Format

The first line should contain the minimum sum of absolute differences. The next nn lines should each contain an integer, representing the sequence values ziz_i.

7
9
4
8
20
14
15
18
13
6
7
8
13
14
15
18

Hint

Constraints

For 100%100\% of the testdata, 1n1061 \le n \le 10^6, 0ti2×1090 \le t_i \le 2 \times 10^9.

Notes

Translated from BalticOI 2004 Day1 C Sequence. Thanks to @TimeTraveller for providing the SPJ.

Translated by ChatGPT 5