Police Operation
Difficulty: Hard

Roy is helping the police department of his city in crime fighting. Today, they informed him about a new planned operation.

Think of the city as a $2D$ plane. The road along the $X$-axis is very crime prone, because $n$ criminals live there. No two criminals live at the same position.

To catch these criminals, the police department has to recruit some police officers and give each of them USD $h$ as wages. A police officer can start his operation from any point $a$, drive his car to point $b$ in a straight line, and catch all the criminals who live on this way. The cost of fuel used by the officer's car is equal to the square of the euclidean distance between points $a$ and $b$ (Euclidean distance between points $(x_1,y_1)$ and $(x_2,y_2)$ equals to $\sqrt{ (x_1-x_2)^2 + (y_1-y_2)^2 }$ ).

The police department asks Roy to plan this operation. So Roy has to tell them the number of officers to recruit and the routes these officers should take in order to catch all the criminals. Roy has to provide this information while minimizing the total expenses of this operation.

Find out the minimum amount of money required to complete the operation.


Input Format:
The first line contains two integers $n$ $(0 \le n \le 2 \times 10^{6})$, number of criminals, and $h$ $( 0 \le h \le 10^{9} )$, the cost of recruiting a police officer. The next line contains $n$ space separated integers. The $i^{th}$ integer indicates the position of the $i^{th}$ criminal on $X$-axis (in other words, if the $i^{th}$ integer is $x$, then location of the $i^{th}$ criminal is $(x,0)$). The value of the positions are between $1$ and $10^9$ and are given in increasing order in the input.


Output Format:
Print the minimum amount of money required to complete the operation.


Sample Input:
    5 10
    1 4 5 6 9


Sample Output:
    34


Python 3 starter code:
#
# Complete the 'policeOperation' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. INTEGER h
#  2. INTEGER_ARRAY criminals
#

def policeOperation(h, criminals):
    # Write your code here




Reference solution (C++):
###C++  
```cpp  
#ifdef ssu1
#define _GLIBCXX_DEBUG
#endif
#undef NDEBUG

#include <algorithm>
#include <functional>
#include <numeric>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <bitset>
#include <sstream>

using namespace std;

#define fore(i, l, r) for(int i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define sz(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define X first
#define Y second

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template<typename T> T abs(T a) { return a < 0 ? -a : a; }
template<typename T> T sqr(T a) { return a*a; }

const int INF = (int)1e9;
const ld EPS = 1e-9;
const ld PI = 3.1415926535897932384626433832795;

/*

    This is just to check correctness of input

*/
int readInt(int l, int r){
    int x;
    if(scanf("%d", &x) != 1){
        fprintf(stderr, "Expected int in range [%d, %d], but haven't found!", l, r);
        throw;
    }
    if(!(l <= x && x <= r)){
        fprintf(stderr, "Expected int in range [%d, %d], but found %d!", l, r, x);
        throw;
    }
    return x;
}
int readInt(int l, int r, string name){
    int x;
    if(scanf("%d", &x) != 1){
        fprintf(stderr, "Expected int %s in range [%d, %d], but haven't found!", name.c_str(), l, r);
        throw;
    }
    if(!(l <= x && x <= r)){
        fprintf(stderr, "Expected int %s in range [%d, %d], but found %d!", name.c_str(), l, r, x);
        throw;
    }
    return x;
}
li readLong(li l, li r){
    li x;
    if(scanf("%lld", &x) != 1){
        fprintf(stderr, "Expected long long in range [%lld, %lld], but haven't found!", l, r);
        throw;
    }
    if(!(l <= x && x <= r)){
        fprintf(stderr, "Expected long long in range [%lld, %lld], but found %lld!", l, r, x);
        throw;
    }
    return x;
}
li readLong(li l, li r, string name){
    li x;
    if(scanf("%lld", &x) != 1){
        fprintf(stderr, "Expected long long %s in range [%lld, %lld], but haven't found!", name.c_str(), l, r);
        throw;
    }
    if(!(l <= x && x <= r)){
        fprintf(stderr, "Expected long long %s in range [%lld, %lld], but found %lld!", name.c_str(), l, r, x);
        throw;
    }
    return x;
}
const ld __EPS__ = 1e-15;
ld readDouble(double l, double r){
    double x;
    if(scanf("%lf", &x) != 1){
        fprintf(stderr, "Expected double in range [%lf, %lf], but haven't found!", l, r);
        throw;
    }
    if(!(l <= x + __EPS__ && x <= r + __EPS__)){
        fprintf(stderr, "Expected double in range [%lf, %lf], but found %lf!", l, r, x);
        throw;
    }
    return x;
}
ld readDouble(double l, double r, string name){
    double x;
    if(scanf("%lf", &x) != 1){
        fprintf(stderr, "Expected double %s in range [%lf, %lf], but haven't found!", name.c_str(), l, r);
        throw;
    }
    if(!(l <= x + __EPS__ && x <= r + __EPS__)){
        fprintf(stderr, "Expected double %s in range [%lf, %lf], but found %lf!", name.c_str(), l, r, x);
        throw;
    }
    return x;
}

struct line{
    li A, B;
    int lf, rg;
    line(){}
    li eval(li x) const {
        return A * x + B;
    }
};

const int NMAX = 2500000, MAXX = int(1e9) + 100;

int n, C, x[NMAX];
li d[NMAX];

bool worse(const line& a, const line& b, li x){
    return a.eval(x) >= b.eval(x);
}

bool worse(const line& a, const line& b){
    return worse(a, b, a.lf) && worse(a, b, a.rg);
}

void add(vector<line>& q, int idx){
    line cur;
    cur.B = d[idx] + x[idx] * 1LL * x[idx];
    cur.A = -2LL * x[idx];

    while(!q.empty() && worse(q.back(), cur)){
        q.pop_back();
    }

    if(q.empty())
        cur.lf = 1, cur.rg = MAXX;
    else{
        cur.lf = q.back().rg + 1;
        cur.rg = MAXX;
        if(worse(q.back(), cur, q.back().rg)){
            assert(!worse(q.back(), cur, q.back().lf));
            int lf = q.back().lf, rg = q.back().rg;
            while(rg - lf > 1){
                int mid = (lf + rg) >> 1;
                if(worse(q.back(), cur, mid))
                    rg = mid;
                else
                    lf = mid;
            }
            q.back().rg = lf;
            cur.lf = rg;
        }
    }
    if(cur.lf <= cur.rg)
        q.pb(cur);
}

bool cmp(const line& o1, const line& o2){
    return o1.lf < o2.lf;
}

li get_val(const vector<line>& q, int x){
    line tmp;
    tmp.lf = x;
    int pos = int(upper_bound(all(q), tmp, cmp) - q.begin());
    pos--;
    assert(q[pos].lf <= x && x <= q[pos].rg);
    return q[pos].eval(x);
}

int main(){
#ifdef ssu1
    assert(freopen("input.txt", "rt", stdin));
    //assert(freopen("output.txt", "wt", stdout));
#endif

    n = readInt(0, 2000000);
    C = readInt(0, int(1e9));
    forn(i, n){
        x[i] = readInt(1, int(1e9));
    }
    forn(i, n - 1){
        assert(x[i] <= x[i + 1]);
    }
    n = int(unique(x, x + n) - x);

    if(n == 0){
        puts("0");
        exit(0);
    }

    vector<line> q;

    d[0] = 0;
    add(q, 0);

    fore(i, 1, n + 1){
        d[i] = get_val(q, x[i - 1]) + x[i - 1] * 1LL * x[i - 1] + C;
        add(q, i); 
    }

    cout << d[n] << endl;
    return 0;
}
```