Problem:
Score : 100 points

Problem Statement

There are N stones, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), the height of Stone i is h_i.
Here, h_1 < h_2 < \cdots < h_N holds.

There is a frog who is initially on Stone 1.
He will repeat the following action some number of times to reach Stone N:

If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

All values in input are integers.

2 \leq N \leq 2 \times 10^5

1 \leq C \leq 10^{12}

1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6

Input

Input is given from Standard Input in the following format:

N C
h_1 h_2 \ldots h_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

5 6
1 2 3 4 5

Sample Output 1

20

If we follow the path 1 → 3 → 5, the total cost incurred would be ((3 - 1)^2 + 6) + ((5 - 3)^2 + 6) = 20.

Sample Input 2

2 1000000000000
500000 1000000

Sample Output 2

1250000000000

The answer may not fit into a 32-bit integer type.

Sample Input 3

8 5
1 3 4 5 10 11 12 13

Sample Output 3

62

If we follow the path 1 → 2 → 4 → 5 → 8, the total cost incurred would be ((3 - 1)^2 + 5) + ((5 - 3)^2 + 5) + ((10 - 5)^2 + 5) + ((13 - 10)^2 + 5) = 62.

Solution in C (GCC 5.4.1) (judge verdict: Wrong Answer)

// MattG 1-21-2020 (2:47 AM)

#include <stdio.h>

#define MAX 200005

typedef unsigned long long ull;

ull max(ull a, ull b) {
    return a > b ? a : b;
}

ull min(ull a, ull b) {
    return a < b ? a : b;
}

int abs(int a) {
    return a >= 0 ? a : -a;
}

ull sqr(ull a) {
    return a * a;
}

typedef struct {
    int index;
    ull height;
} Entry;

ull get_overtake(int *h, ull *dp, Entry old, int x) {
    ull top = ( sqr(h[x]) - sqr(h[old.index]) + dp[x] - dp[old.index] );
    ull bottom = ( 2 * ( h[x] - h[old.index] ) );  
    return (top / bottom) + (top % bottom == 0 ? 0 : 1);
    // account for not divisible case
}

// return new min_end value
//    0 1 2 3 4 5 6 _   <- min_list
//      s         e     // check endpoint cases separately (or at least end)
// if we're on an index, we're considering whether to replace it or not

int binsert(int s, int e, int *h, ull *dp, Entry *min_list, int new_index) {

    // how many left? implement todo? maybe unnecessary
    
    int m = (s + e) / 2;
    ull ot = get_overtake(h, dp, min_list[m-1], new_index);
    if (ot > min_list[m].height) {   // go right
        return binsert(m+1, e, h, dp, min_list, new_index);
    }
    else if (ot < h[new_index]) {  // go left
        return binsert(s, m-1, h, dp, min_list, new_index);
    }
    else {
        min_list[m].index = new_index;
        min_list[m].height = ot;
        return m;
    }
}

int main() {
    int N;
    ull C;
    int h[MAX];
    ull dp[MAX];
    Entry min_list[MAX];
    int min_start = 0;
    int min_end = 0;
    int src_index, start_check, end_check;

    dp[0] = 0;
    min_list[0].index = 0;
    min_list[0].height = -1;

    scanf("%d %llu", &N, &C);
    scanf("%d", &h[0]);

    for (int i=1; i < N; i++) {
        scanf("%d", &h[i]);
        // update min_start index (here or end?) // might be wrong be careful
        while (min_start < min_end && h[i] >= min_list[min_start+1].height) {
            min_start++;
        }

        src_index = min_list[min_start].index;
        dp[i] = dp[src_index] + sqr(h[i] - h[src_index]) + C;

        start_check = get_overtake(h, dp, min_list[min_start], i);
        if (start_check <= h[i]) {
            min_list[min_start].index = i;
            min_list[min_start].height = -1;
            min_end = min_start;
        }
        else {
            end_check = get_overtake(h, dp, min_list[min_end], i);
            if (end_check >= h[i]) {
                min_end++;
                min_list[min_end].index = i;
                min_list[min_end].height = end_check;
            }
            else {
                min_end = binsert(min_start+1, min_end, h, dp, min_list, i);
            }
        }
    }

    printf("%llu\n", dp[N-1]);
    return 0;
}