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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

/**
 * @author derrick20
 */
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String args[]) throws Exception {
        FastScanner sc = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);

        int N = sc.nextInt();
        long C = sc.nextLong();
        long[] height = new long[N];
        for (int i = 0; i < N; i++) {
            height[i] = sc.nextLong();
        }

        LinkedList<Line> deque = new LinkedList<>();
        // Our DP states are a given height and the previous cost
        // Since cost up to index j =
        // (h[j] - h[i])^2 + dpPrev + C
        // h[j]^2 - 2h[i]h[j] + (h[i]^2 + dpPrev + C)
        // When comparing two lines with different starting i's, ending at a specific j,
        // the second part is independent, so that can all be lumped into the prevCost (y-intercept)
        // What remains is:
        // h[j]^2 - 2h[i]h[j] + prevCost
        // However, if h[j] is fixed (since we are testing the cost at a specific h[j],
        // Then that will cancel out when we subtract the two cost values for different lines.
        // So, all we need to do is compare the following:
        // -2h[i1]h[j] + prevCost1 ?= -2h[i2]h[j] + prevCost2
        // This perfectly represents a line!
        // **NOTE we must be sure to not forget the h[j] term of the line!

        deque.add(new Line(-2 * height[0], square(height[0])));
        for (int i = 1; i < N; i++) {
            // Need to maintain two invariants:
            // First, before we use the solution bag to retrieve the best previous
            // partial solution from which to reach this position, we need tha front
            // of the deque to have the minimum cost to reach this position. (Since
            // the lines are all more and more negative slopes, if the current front cost
            // isn't the minimum, it will never be the minimum later. Basically, the line
            // has intersected with the next line (starting slightly later), which means
            // that next line is now better. Essentially, initially, starting from a stone
            // earlier in position is better due to the reduced fixed cost of switching
            // stones. However, at a certain point, the quadratic cost due to increasing
            // height difference will cause the cost to always be worse.
            // Invariant: i < j => cost(i to curr) < cost(j to curr)
            long currHeight = height[i];
            while (deque.size() >= 2 && deque.get(0).getCost(currHeight) > deque.get(1).getCost(currHeight)) {
                deque.pollFirst();
            }
            // Now, the front has the best partial solution to jump from.
            Line start = deque.getFirst();
            long costToHere = start.getCost(currHeight) + square(currHeight);
            // The logic of this is interesting:
            // We associate h[i]^2 with each state starting at h[i]. We leave out the h[j]
            // term for where it'll end, and only compensate by adding that in once we finally
            // decide on where to end by using the invariant deque.

            // So, it just so happens that we need to do that first association and then
            // the compensation at the same time, which means we add h[j]^2 TWICE, for two
            // distinct reasons. We are always doing this, expecting to jump to a future
            // state. So, when we finally reach the answer, we added it an extra time, and need
            // to subtract. (Since we no longer need to do anything to prepare for future
            // DP states, so it's an artifact of our association method)
            long lumpedPrevCost = square(currHeight) + costToHere + C;
            Line next = new Line(-2 * currHeight, lumpedPrevCost);

            // Now we need to maintain the second invariant:
            // The intersection points of the cost functions must be such that
            // the last two lines must intersect the 3rd from last line in ORDER
            // Invariant: i < j < k => inter(i, j) < inter(i, k)
            // Basically, the nextLine will keep barreling through previous lines
            // with later intersection times.
            while (deque.size() >= 2 && !isValid(deque.get(deque.size() - 2), deque.getLast(), next)) {
                deque.pollLast();
            }
            // Now, the invariant is held, so the added line will be correctly placed
            deque.addLast(next);
        }
        Line end = deque.getLast();
        long totalCost = end.prevCost - square(height[N - 1]);
        out.println(totalCost);
        out.close();
    }

    static long square(long val) {
        return val * val;
    }

    static boolean isValid(Line prev, Line curr, Line next) {
        // Need to compare the two intersection points:
        // y1 = neg2h1 * hInt + prevCost1
        // y2 = neg22h2 * hInt + prevCost2
        // hInt for 1 and 2 = (prevCost1 - prevCost2) / (neg2h2 - neg2h1)
        // hInt for 1 and 3 = (prevCost1 - prevCost3) / (neg2h3 - neg2h1)
        // hInt(1, 2) < hInt(1, 3)
        // (prevCost1 - prevCost2) * (h3 - h1) < (prevCost1 - prevCost3) * (h2 - h1)
        return (prev.prevCost - curr.prevCost) * (next.neg2startH - prev.neg2startH) < (prev.prevCost - next.prevCost) * (curr.neg2startH - prev.neg2startH);
    }

    static class Line {
        long neg2startH, prevCost;
        public Line(long myNeg2StartH, long myPrevCost) {
            neg2startH = myNeg2StartH;
            prevCost = myPrevCost;
        }

        public long getCost(long nextHeight) {
            return neg2startH * nextHeight + prevCost;
        }

        public String toString() {
            return neg2startH + ", " + prevCost;
        }
    }

    static class FastScanner {
        public int BS = 1<<16;
        public char NC = (char)0;
        byte[] buf = new byte[BS];
        int bId = 0, size = 0;
        char c = NC;
        double cnt = 1;
        BufferedInputStream in;

        public FastScanner() {
            in = new BufferedInputStream(System.in, BS);
        }

        public FastScanner(String s) {
            try {
                in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
            }
            catch (Exception e) {
                in = new BufferedInputStream(System.in, BS);
            }
        }

        private char getChar(){
            while(bId==size) {
                try {
                    size = in.read(buf);
                }catch(Exception e) {
                    return NC;
                }
                if(size==-1)return NC;
                bId=0;
            }
            return (char)buf[bId++];
        }

        public int nextInt() {
            return (int)nextLong();
        }

        public long nextLong() {
            cnt=1;
            boolean neg = false;
            if(c==NC)c=getChar();
            for(;(c<'0' || c>'9'); c = getChar()) {
                if(c=='-')neg=true;
            }
            long res = 0;
            for(; c>='0' && c <='9'; c=getChar()) {
                res = (res<<3)+(res<<1)+c-'0';
                cnt*=10;
            }
            return neg?-res:res;
        }

        public double nextDouble() {
            double cur = nextLong();
            return c!='.' ? cur:cur+nextLong()/cnt;
        }

        public String next() {
            StringBuilder res = new StringBuilder();
            while(c<=32)c=getChar();
            while(c>32) {
                res.append(c);
                c=getChar();
            }
            return res.toString();
        }

        public String nextLine() {
            StringBuilder res = new StringBuilder();
            while(c<=32)c=getChar();
            while(c!='\n') {
                res.append(c);
                c=getChar();
            }
            return res.toString();
        }

        public boolean hasNext() {
            if(c>32)return true;
            while(true) {
                c=getChar();
                if(c==NC)return false;
                else if(c>32)return true;
            }
        }
    }
}