CF1614B.Divan and a New Project
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
The company "Divan's Sofas" is planning to build n+1 different buildings on a coordinate line so that:
- the coordinate of each building is an integer number;
- no two buildings stand at the same point.
Let xi be the coordinate of the i -th building. To get from the building i to the building j , Divan spends ∣xi−xj∣ minutes, where ∣y∣ is the absolute value of y .
All buildings that Divan is going to build can be numbered from 0 to n . The businessman will live in the building 0 , the new headquarters of "Divan's Sofas". In the first ten years after construction Divan will visit the i -th building ai times, each time spending 2⋅∣x0−xi∣ minutes for walking.
Divan asks you to choose the coordinates for all n+1 buildings so that over the next ten years the businessman will spend as little time for walking as possible.
输入格式
Each test contains several test cases. The first line contains one integer number t ( 1≤t≤103 ) — the number of test cases.
The first line of each case contains an integer n ( 1≤n≤2⋅105 ) — the number of buildings that "Divan's Sofas" is going to build, apart from the headquarters.
The second line contains the sequence a1,a2,…,an ( 0≤ai≤106 ), where ai is the number of visits to the i -th building.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 .
输出格式
For each test case, on the first line print the number T — the minimum time Divan will spend walking.
On the second line print the sequence x0,x1,…,xn of n+1 integers, where xi ( −106≤xi≤106 ) is the selected coordinate of the i -th building. It can be shown that an optimal answer exists with coordinates not exceeding 106 .
If there are multiple answers, print any of them.
输入输出样例
输入#1
4 3 1 2 3 5 3 8 10 6 1 5 1 1 1 1 1 1 0
输出#1
14 2 4 1 3 78 1 -1 0 2 3 4 18 3 6 1 5 2 4 0 1 2
说明/提示
Let's look at the first example.
Divan will visit the first building a1=1 times, the second a2=2 times and the third a3=3 times. Then one of the optimal solution will be as follows:
- the headquarters is located in x0=2 ;
- x1=4 : Divan will spend 2⋅∣x0−x1∣⋅a1=2⋅∣2−4∣⋅1=4 minutes walking to the first building;
- x2=1 : Divan will spend 2⋅∣x0−x2∣⋅a2=2⋅∣2−1∣⋅2=4 minutes walking to the second building;
- x3=3 : Divan will spend 2⋅∣x0−x3∣⋅a3=2⋅∣2−3∣⋅3=6 minutes walking to the third building.
In total, Divan will spend 4+4+6=14 minutes. It can be shown that it is impossible to arrange buildings so that the businessman spends less time.
Among others, x=[1,3,2,0] , x=[−5,−3,−6,−4] are also correct answers for the first example.