<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

524. Buoys
Time limit per test: 0.5 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard



The swimming area of Berhattan's city beach is marked out with n buoys. The buoys form a straight line. When the buoys were being put into the water, nobody cared to observe the same distance between each pair of adjacent buoys.

Now the beach keeper wants the distance between any two adjacent buoys to be the same. He plans to shift some or all of the buoys without changing their respective order. To facilitate the task, he wants the total length of all shifts to be as small as possible.

Given coordinates of the buoys, you should find the minimum possible length of all shifts, as well as new coordinates of the buoys.

Input
The first line of input contains a single integer n (2 ≤ n ≤ 400), n — the number of buoys. The second line contains buoys' integer coordinates x1, x2,..., xn (-10000 ≤ xi ≤ 10000). No two given buoys will share the same place. The coordinates are given in strictly increasing order.

Output
To the first line print a real number t — the minimum possible total length of required shifts. Output this value with at least 4 digits after the decimal point.

To the second line print n numbers — new coordinates of the buoys. The new coordinates should be printed in strictly increasing order with at least 7 digits after the decimal point. If there are several optimal ways to shift the buoys, you may output any of them.

Example(s)
sample input
sample output
4 -2 2 6 9 
1.0000
-2.0000000000 1.6666666667 5.3333333333 9.0000000000 



Note
All buoys are located on the Ox axis. You may move buoys only along the Ox axis.

<|response|>
1. Abridged Problem Statement  
  
You are given n distinct points x[0…n−1] on the real line in strictly increasing order. You want to move them (without changing their order) so that they form an arithmetic progression  
 
  y[i] = L + d·i,  for i = 0…n−1,  with d ≥ 0,  
 
minimizing the total movement  
 
  T = ∑ᵢ |x[i] − y[i]|.  
 
Output the minimum T and one choice of (L, d) (hence the y[i]) achieving it.

2. Key Observations  
  
•  For a fixed common difference d, define b[i] = x[i] − d·i.  
   Then  
 
  T(L; d) = ∑ᵢ |x[i] − (L + d·i)|  
      = ∑ᵢ |b[i] − L|.  
 
   As a function of L alone, this is minimized by choosing L to be any median of the multiset {b[i]}.  
 
•  Let F(d) = min_L T(L; d).  One can compute F(d) in O(n) (or O(n log n) to find the median).  
 
•  One can show F(d) is a convex function of d.  Therefore we can apply a ternary search on d over [0, Dmax] (e.g. Dmax = 2·10⁴ or 10⁷ to be safe) to find the d minimizing F(d).  

3. Full Solution Approach  
  
1. Read n and the array x[0…n−1].  
2. Fix a search interval for d, say [0, 1e7].  
3. Repeat ~100 iterations of ternary search:  
   a. Let m1 = l + (r−l)/3, m2 = r − (r−l)/3.  
   b. For each of m1 and m2, build b[i] = x[i] − d·i, sort b, pick L = b[n/2], compute cost = ∑|b[i] − L|.  
   c. Compare the two costs: shrink [l,r] accordingly and keep track of the best (d,L,cost).  
4. After the search, recompute the best L and cost at the best d more precisely.  
5. Output cost (with ≥4 decimals) and the sequence y[i] = L + d·i (with ≥7 decimals).  

Complexity: Each cost-evaluation is O(n log n) for sorting. We do O(100) evaluations → O(100·n log n), which is fine for n ≤ 400.

4. C++ Implementation with Detailed Comments  

```cpp
#include <bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<long double> x(n);
    for(int i = 0; i < n; i++){
        double xi; 
        cin >> xi;
        x[i] = xi;
    }

    // Ternary-search range for d
    long double lo = 0.0L, hi = 1e7L;
    long double best_d = 0, best_L = 0, best_cost = 1e300;

    // Helper lambda: for fixed d, compute optimal L (the median of b[i]) and total cost
    auto eval = [&](long double d) {
        vector<long double> b(n);
        for(int i = 0; i < n; i++){
            b[i] = x[i] - d * i;
        }
        sort(b.begin(), b.end());
        long double L = b[n/2];  // median
        long double cost = 0;
        for(int i = 0; i < n; i++){
            cost += fabsl(b[i] - L);
        }
        return pair<long double,long double>(cost, L);
    };

    // Perform ~100 iterations of ternary search on d
    for(int iter = 0; iter < 100; iter++){
        long double m1 = lo + (hi - lo) / 3.0L;
        long double m2 = hi - (hi - lo) / 3.0L;

        auto r1 = eval(m1);
        auto r2 = eval(m2);

        // Keep track of the globally best
        if(r1.first < best_cost){
            best_cost = r1.first;
            best_d = m1;
            best_L = r1.second;
        }
        if(r2.first < best_cost){
            best_cost = r2.first;
            best_d = m2;
            best_L = r2.second;
        }

        // Narrow the search range
        if(r1.first < r2.first){
            hi = m2;
        } else {
            lo = m1;
        }
    }

    // Recompute best_L and best_cost at the best_d for full accuracy
    auto final_pair = eval(best_d);
    best_cost = final_pair.first;
    best_L    = final_pair.second;

    // Output total minimal shift with at least 4 decimal places
    cout << fixed << setprecision(4) << best_cost << "\n";

    // Output new coordinates y[i] = L + d*i with at least 7 decimals
    cout << setprecision(7);
    for(int i = 0; i < n; i++){
        long double y = best_L + best_d * i;
        cout << y << (i+1<n ? ' ' : '\n');
    }

    return 0;
}
```

5. Python Implementation with Detailed Comments  

```python
import sys

def read_input():
    data = sys.stdin.read().strip().split()
    n = int(data[0])
    x = list(map(float, data[1:1+n]))
    return n, x

def eval_d(x, d):
    """
    For a fixed d, build b[i] = x[i] - d*i,
    pick L = median(b), and return (cost, L).
    """
    b = [xi - d*i for i, xi in enumerate(x)]
    b.sort()
    L = b[len(b)//2]
    cost = sum(abs(bi - L) for bi in b)
    return cost, L

def main():
    n, x = read_input()

    lo, hi = 0.0, 1e7
    best_cost = float('inf')
    best_d = 0.0
    best_L = 0.0

    # Ternary search over d
    for _ in range(100):
        m1 = lo + (hi - lo) / 3.0
        m2 = hi - (hi - lo) / 3.0

        cost1, L1 = eval_d(x, m1)
        cost2, L2 = eval_d(x, m2)

        # Update global best
        if cost1 < best_cost:
            best_cost, best_d, best_L = cost1, m1, L1
        if cost2 < best_cost:
            best_cost, best_d, best_L = cost2, m2, L2

        # Narrow the interval
        if cost1 < cost2:
            hi = m2
        else:
            lo = m1

    # Recompute for final best_d to ensure precision
    best_cost, best_L = eval_d(x, best_d)

    # Print results
    #  - total shift with ≥4 decimals
    print(f"{best_cost:.4f}")
    #  - new positions y[i] with ≥7 decimals
    y = [best_L + best_d * i for i in range(n)]
    print(" ".join(f"{yi:.7f}" for yi in y))

if __name__ == "__main__":
    main()
```

Explanation of Key Steps:  
- We reduce the two‐parameter minimization over (L, d) to a one‐parameter search in d by observing that for each fixed d, the best L is the median of shifted points b[i].  
- Convexity of the cost in d justifies a ternary search to high precision.  
- Finally, we reconstruct the optimal L and output the aligned arithmetic progression.