Language: C++

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Diagnostics;
using System.Numerics;
using static System.Console;
using static System.Convert;
using static System.Math;
using static Template;
using Pi = Pair<int, int>;

class Solver
{
    public void Solve(Scanner sc)
    {
        int N;long C;
        sc.Make(out N, out C);
        var cn = new ConvexHullTrick_Monotone();
        var H = sc.ArrLong;
        var dp = new long[N];
        cn.Add(-2 * H[0], H[0] * H[0] + C);
        for (var i = 1; i < N; i++)
        {
            dp[i] = cn.Query_inc(H[i])+H[i]*H[i];
            cn.Add(-2 * H[i], H[i] * H[i] + C + dp[i]);
        }
        WriteLine(dp[N-1]);
    }
}

class ConvexHullTrick_Monotone
{
    bool isMin;
    Deque<Line> lines;
    public ConvexHullTrick_Monotone(bool isMin = true)
    {
        this.isMin = isMin;
        lines = new Deque<Line>();
    }
    bool CanErase(Line l1, Line l2, Line l3) => (l3.B - l2.B) * (l2.A - l1.A) >= (l2.B - l1.B) * (l3.A - l2.A);
    public void Add(long A, long B)
    {
        if (!isMin) { A *= -1; B *= -1; }
        var L = new Line(A, B);
        if (lines.Count == 0)
        {
            lines.PushHead(L); return;
        }
        if (lines[0].A <= A)
        {
            if (lines[0].A == A)
            {
                if (lines[0].B <= B) return;
                lines.PopHead();
            }
            while (lines.Count >= 2 && CanErase(L, lines[0], lines[1]))
                lines.PopHead();
            lines.PushHead(L);
        }
        else
        {
            if (lines[lines.Count - 1].A == A)
            {
                if (lines[lines.Count - 1].B <= B) return;
                lines.PopTail();
            }
            while (lines.Count >= 2 && CanErase(lines[lines.Count - 2], lines[lines.Count - 1], L))
                lines.PopTail();
            lines.PushTail(L);
        }
    }
    long Calc(int i, long x) => lines[i].A * x + lines[i].B;
    public long Query(long x)
    {
        int r = lines.Count - 1, l = -1;
        while (r - l > 1)
        {
            var m = (r + l) >> 1;
            if (Calc(m, x) <= Calc(m + 1, x)) r = m;
            else l = m;
        }
        return (isMin ? 1 : -1) * Calc(r, x);
    }
    public long Query_inc(long x)
    {
        while (lines.Count >= 2 && Calc(0, x) >= Calc(1, x)) lines.PopHead();
        return (isMin ? 1 : -1) * Calc(0, x);
    }
    public long Query_dec(long x)
    {
        while (lines.Count >= 2 && Calc(lines.Count - 1, x) >= Calc(lines.Count - 2, x)) lines.PopTail();
        return (isMin ? 1 : -1) * Calc(lines.Count - 1, x);
    }
    private struct Line
    {
        public long A, B;
        public Line(long A, long B) { this.A = A; this.B = B; }
        public int CompareTo(Line l2) => A == l2.A ? B.CompareTo(l2.B) : A.CompareTo(l2.A);
    }
}
public class Deque<T>
{
    private T[] data;
    int offset, sz;
    public int Count { get; private set; }
    public Deque() { data = new T[sz = 16]; }
    public T this[int i]
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        get { return data[MOD(i + offset)]; }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        set { data[MOD(i + offset)] = value; }
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public int MOD(int v) => v & (sz - 1);
    private void Extend()
    {
        var t = new T[sz << 1];
        for (var i = 0; i < sz; i++)
            t[i] = data[MOD(offset + i)];
        offset = 0;
        data = t;
        sz <<= 1;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public void PushHead(T item)
    {
        if (Count == sz) Extend();
        data[MOD(--offset)] = item;
        Count++;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public T PopHead()
    {
        if (Count == 0) return default(T);
        Count--;
        return data[MOD(offset++)];
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public void PushTail(T item)
    {
        if (Count == sz) Extend();
        data[MOD(Count++ + offset)] = item;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public T PopTail()
    {
        if (Count == 0) return default(T);
        return data[MOD(--Count + offset)];
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public bool Any() => Count > 0;
    public T[] ToArray()
    {
        var rt = new T[Count];
        for (int i = 0; i < Count; i++)
            rt[i] = this[i];
        return rt;
    }
}
#region Template
public static class Template
{
    static void Main(string[] args)
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        new Solver().Solve(new Scanner());
        Console.Out.Flush();
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) == 1) { a = b; return true; } return false; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) == -1) { a = b; return true; } return false; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void swap<T>(this IList<T> A, int i, int j) { var t = A[i]; A[i] = A[j]; A[j] = t; }
    public static T[] Shuffle<T>(this IList<T> A) { T[] rt = A.ToArray(); Random rnd = new Random(); for (int i = rt.Length - 1; i >= 1; i--) swap(ref rt[i], ref rt[rnd.Next(i + 1)]); return rt; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static T[] Create<T>(int n, Func<T> f) { var rt = new T[n]; for (var i = 0; i < rt.Length; ++i) rt[i] = f(); return rt; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static T[] Create<T>(int n, Func<int, T> f) { var rt = new T[n]; for (var i = 0; i < rt.Length; ++i) rt[i] = f(i); return rt; }
    public static void Fail<T>(T s) { Console.WriteLine(s); Console.Out.Close(); Environment.Exit(0); }

}
public class Scanner
{
    public string Str => Console.ReadLine().Trim();
    public int Int => int.Parse(Str);
    public long Long => long.Parse(Str);
    public double Double => double.Parse(Str);
    public int[] ArrInt => Str.Split(' ').Select(int.Parse).ToArray();
    public long[] ArrLong => Str.Split(' ').Select(long.Parse).ToArray();
    public char[][] Grid(int n) => Create(n, () => Str.ToCharArray());
    public int[] ArrInt1D(int n) => Create(n, () => Int);
    public long[] ArrLong1D(int n) => Create(n, () => Long);
    public int[][] ArrInt2D(int n) => Create(n, () => ArrInt);
    public long[][] ArrLong2D(int n) => Create(n, () => ArrLong);
    private Queue<string> q = new Queue<string>();
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public T Next<T>() { if (q.Count == 0) foreach (var item in Str.Split(' ')) q.Enqueue(item); return (T)Convert.ChangeType(q.Dequeue(), typeof(T)); }
    public void Make<T1>(out T1 v1) => v1 = Next<T1>();
    public void Make<T1, T2>(out T1 v1, out T2 v2) { v1 = Next<T1>(); v2 = Next<T2>(); }
    public void Make<T1, T2, T3>(out T1 v1, out T2 v2, out T3 v3) { Make(out v1, out v2); v3 = Next<T3>(); }
    public void Make<T1, T2, T3, T4>(out T1 v1, out T2 v2, out T3 v3, out T4 v4) { Make(out v1, out v2, out v3); v4 = Next<T4>(); }
    public void Make<T1, T2, T3, T4, T5>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5) { Make(out v1, out v2, out v3, out v4); v5 = Next<T5>(); }
    public void Make<T1, T2, T3, T4, T5, T6>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5, out T6 v6) { Make(out v1, out v2, out v3, out v4, out v5); v6 = Next<T6>(); }
    //public (T1, T2) Make<T1, T2>() { Make(out T1 v1, out T2 v2); return (v1, v2); }
    //public (T1, T2, T3) Make<T1, T2, T3>() { Make(out T1 v1, out T2 v2, out T3 v3); return (v1, v2, v3); }
    //public (T1, T2, T3, T4) Make<T1, T2, T3, T4>() { Make(out T1 v1, out T2 v2, out T3 v3, out T4 v4); return (v1, v2, v3, v4); }
}
public class Pair<T1, T2> : IComparable<Pair<T1, T2>>
{
    public T1 v1;
    public T2 v2;
    public Pair() { }
    public Pair(T1 v1, T2 v2)
    { this.v1 = v1; this.v2 = v2; }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public int CompareTo(Pair<T1, T2> p)
    {
        var c = Comparer<T1>.Default.Compare(v1, p.v1);
        if (c == 0)
            c = Comparer<T2>.Default.Compare(v2, p.v2);
        return c;
    }
    public override string ToString() => $"{v1.ToString()} {v2.ToString()}";
    public void Deconstruct(out T1 a, out T2 b) { a = v1; b = v2; }
}

public class Pair<T1, T2, T3> : Pair<T1, T2>, IComparable<Pair<T1, T2, T3>>
{
    public T3 v3;
    public Pair() : base() { }
    public Pair(T1 v1, T2 v2, T3 v3) : base(v1, v2)
    { this.v3 = v3; }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public int CompareTo(Pair<T1, T2, T3> p)
    {
        var c = base.CompareTo(p);
        if (c == 0)
            c = Comparer<T3>.Default.Compare(v3, p.v3);
        return c;
    }
    public override string ToString() => $"{base.ToString()} {v3.ToString()}";
    public void Deconstruct(out T1 a, out T2 b, out T3 c) { Deconstruct(out a, out b); c = v3; }
}
#endregion

Compiler output:
a.cc:164:2: error: invalid preprocessing directive #region
  164 | #region Template
      |  ^~~~~~
a.cc:253:2: error: invalid preprocessing directive #endregion
  253 | #endregion
      |  ^~~~~~~~~
a.cc:1:7: error: expected nested-name-specifier before 'System'
    1 | using System;
      |       ^~~~~~
a.cc:2:7: error: expected nested-name-specifier before 'System'
    2 | using System.Collections.Generic;
      |       ^~~~~~
a.cc:3:7: error: expected nested-name-specifier before 'System'
    3 | using System.Linq;
      |       ^~~~~~
a.cc:4:7: error: expected nested-name-specifier before 'System'
    4 | using System.IO;
      |       ^~~~~~
a.cc:5:7: error: expected nested-name-specifier before 'System'
    5 | using System.Runtime.CompilerServices;
      |       ^~~~~~
a.cc:6:7: error: expected nested-name-specifier before 'System'
    6 | using System.Text;
      |       ^~~~~~
a.cc:7:7: error: expected nested-name-specifier before 'System'
    7 | using System.Diagnostics;
      |       ^~~~~~
a.cc:8:7: error: expected nested-name-specifier before 'System'
    8 | using System.Numerics;
      |       ^~~~~~
a.cc:9:7: error: expected nested-name-specifier before 'static'
    9 | using static System.Console;
      |       ^~~~~~
a.cc:10:7: error: expected nested-name-specifier before 'static'
   10 | using static System.Convert;
      |       ^~~~~~
a.cc:11:7: error: expected nested-name-specifier before 'static'
   11 | using static System.Math;
      |       ^~~~~~
a.cc:12:7: error: expected nested-name-specifier before 'static'
   12 | using static Template;
      |       ^~~~~~
a.cc:13:12: error: 'Pair' does not name a type
   13 | using Pi = Pair<int, int>;
      |            ^~~~
a.cc:17:11: error: expected ':' before 'void'
   17 |     public void Solve(Scanner sc)
      |           ^~~~~
      |           :
a.cc:17:23: error: 'Scanner' has not been declared
   17 |     public void Solve(Scanner sc)
      |                       ^~~~~~~
a.cc:32:2: error: expected ';' after class definition
   32 | }
      |  ^
      |  ;
a.cc: In member function 'void Solver::Solve(int)':
a.cc:20:12: error: request for member 'Make' in 'sc', which is of non-class type 'int'
   20 |         sc.Make(out N, out C);
      |            ^~~~
a.cc:20:17: error: 'out' was not declared in this scope
   20 |         sc.Make(out N, out C);
      |                 ^~~
a.cc:20:28: error: expected ')' before 'C'
   20 |         sc.Make(out N, out C);
      |                            ^
a.cc:20:16: note: to match this '('
   20 |         sc.Make(out N, out C);
      |                ^
a.cc:21:9: error: 'var' was not declared in this scope
   21 |         var cn = new ConvexHullTrick_Monotone();
      |         ^~~
a.cc:22:13: error: expected ';' before 'H'
   22 |         var H = sc.ArrLong;
      |             ^
a.cc:23:13: error: expected ';' before 'dp'
   23 |         var dp = new long[N];
      |             ^~
a.cc:24:9: error: 'cn' was not declared in this scope
   24 |         cn.Add(-2 * H[0], H[0] * H[0] + C);
      |         ^~
a.cc:24:21: error: 'H' was not declared in this scope
   24 |         cn.Add(-2 * H[0], H[0] * H[0] + C);
      |                     ^
a.cc:25:18: error: expected ';' before 'i'
   25 |         for (var i = 1; i < N; i++)
      |                  ^
a.cc:25:25: error: 'i' was not declared in this scope
   25 |         for (var i = 1; i < N; i++)
      |                         ^
a.cc:27:13: error: 'dp' was not declared in this scope
   27 |             dp[i] = cn.Query_inc(H[i])+H[i]*H[i];
      |             ^~
a.cc:30:19: error: 'dp' was not declared in this scope
   30 |         WriteLine(dp[N-1]);
      |                   ^~
a.cc:30:9: error: 'WriteLine' was not declared in this scope
   30 |         WriteLine(dp[N-1]);
      |         ^~~~~~~~~
a.cc: At global scope:
a.cc:37:5: error: 'Deque' does not name a type
   37 |     Deque<Line> lines;
      |     ^~~~~
a.cc:38:11: error: expected ':' before 'ConvexHullTrick_Monotone'
   38 |     public ConvexHullTrick_Monotone(bool isMin = true)
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~
      |           :
a.cc:43:19: error: 'Line' has not been declared
   43 |     bool CanErase(Line l1, Line l2, Line l3) => (l3.B - l2.B) * (l2.A - l1.A) >= (l2.B - l1.B) * (l3.A - l2.A);
      |                   ^~~~
a.cc:43:28: error: 'Line' has not been declared
   43 |     bool CanErase(Line l1, Line l2, Line l3) => (l3.B - l2.B) * (l2.A - l1.A) >= (l2.B - l1.B) * (l3.A - l2.A);
      |                            ^~~~
a.cc:43:37: error: 'Line' has not been declared
   43 |     bool CanErase(Line l1, Line l2, Line l3) => (l3.B - l2.B) * (l2.A - l1.A) >= (l2.B - l1.B) * (l3.A - l2.A);
      |                                     ^~~~
a.cc:43:49: error: invalid pure specifier (only '= 0' is allowed) before '(' token
   43 |     bool CanErase(Line l1, Line l2, Line l3) => (l3.B - l2.B) * (l2.A - l1.A) >= (l2.B - l1.B) * (l3.A - l2.A);
      |                                                 ^
a.cc:44:11: error: expected ':' before 'void'
   44 |     public void Add(long A, long B)
      |           ^~~~~
      |           :
a.cc:75:33: error: invalid pure specifier (only '= 0' is allowed) before 'lines'
   75 |     long Calc(int i, long x) => lines[i].A * x + lines[i].B;
      |                                 ^~~~~
a.cc:76:11: error: expected ':' before 'long'
   76 |     public long Query(long x)
      |           ^~~~~
      |           :
a.cc:87:11: error: expected ':' before 'long'
   87 |     public long Query_inc(long x)
      |           ^~~~~
      |           :
a.cc:92:11: error: expected ':' before 'long'
   92 |     public long Query_dec(long x)
      |           ^~~~~
      |           :
a.cc:97:12: error: expected ':' before 'struct'
   97 |     private struct Line
      |            ^~~~~~~
      |            :
a.cc:99:15: error: expected ':' before 'long'
   99 |         public long A, B;
      |               ^~~~~
      |               :
a.cc:100:15: error: expected ':' before 'Line'
  100 |         public Line(long A, long B) { this.A = A; this.B = B; }
      |               ^~~~~
      |               :
a.cc:101:15: error: expected ':' before 'int'
  101 |         public int CompareTo(Line l2) => A == l2.A ? B.CompareTo(l2.B) : A.CompareTo(l2.A);
      |               ^~~~
      |               :
a.cc:101:42: error: invalid pure specifier (only '= 0' is allowed) before 'A'
  101 |         public int CompareTo(Line l2) => A == l2.A ? B.CompareTo(l2.B) : A.CompareTo(l2.A);
      |                                          ^
a.cc:102:6: error: expected ';' after struct definition
  102 |     }
      |      ^
      |      ;
a.cc:103:2: error: expected ';' after class definition
  103 | }
      |  ^
      |  ;
a.cc: In constructor 'ConvexHullTrick_Monotone::ConvexHullTrick_Monotone(bool)':
a.cc:40:14: error: request for member 'isMin' in '(ConvexHullTrick_Monotone*)this', which is of pointer type 'ConvexHullTrick_Monotone*' (maybe you meant to use '->' ?)
   40 |         this.isMin = isMin;
      |              ^~~~~
a.cc:41:9: error: 'lines' was not declared in this scope
   41 |         lines = new Deque<Line>();
      |         ^~~~~
a.cc:41:21: error: 'Deque' does not name a type
   41 |         lines = new Deque<Line>();
      |                     ^~~~~
a.cc:41:31: error: expected primary-expression before '>' token
   41 |         lines = new Deque<Line>();
      |                               ^
a.cc:41:33: error: expected primary-expression before ')' token
   41 |         lines = new Deque<Line>();
      |                                 ^
a.cc: In member function 'void ConvexHullTrick_Monotone::Add(long int, long int)':
a.cc:47:9: error: 'var' was not declared in this scope
   47 |         var L = new Line(A, B);
      |         ^~~
a.cc:48:13: error: 'lines' was not declared in this scope
   48 |         if (lines.Count == 0)
      |             ^~~~~
a.cc:50:28: error: 'L' was not declared in this scope
   50 |             lines.PushHead(L); return;
      |                            ^
a.cc:52:13: error: 'lines' was not declared in this scope
   52 |         if (lines[0].A <= A)
      |             ^~~~~
a.cc:59:49: error: 'L' was not declared in this scope
   59 |             while (lines.Count >= 2 && CanErase(L, lines[0], lines[1]))
      |                                                 ^
a.cc:61:28: error: 'L' was not declared in this scope
   61 |             lines.PushHead(L);
      |                            ^
a.cc:70:97: error: 'L' was not declared in this scope
   70 |             while (lines.Count >= 2 && CanErase(lines[lines.Count - 2], lines[lines.Count - 1], L))
      |                                                                                                 ^
a.cc:72:28: error: 'L' was not declared in this scope
   72 |             lines.PushTail(L);
      |                            ^
a.cc: In member function 'long int ConvexHullTrick_Monotone::Query(long int)':
a.cc:78:17: error: 'lines' was not declared in this scope
   78 |         int r = lines.Count - 1, l = -1;
      |                 ^~~~~
a.cc:79:20: error: 'l' was not declared in this scope
   79 |         while (r - l > 1)
      |                    ^
a.cc:81:13: error: 'var' was not declared in this scope
   81 |             var m = (r + l) >> 1;
      |             ^~~
a.cc:82:22: error: 'm' was not declared in this scope
   82 |             if (Calc(m, x) <= Calc(m + 1, x)) r = m;
      |                      ^
a.cc: In member function 'long int ConvexHullTrick_Monotone::Query_inc(long int)':
a.cc:89:16: error: 'lines' was not declared in this scope
   89 |         while (lines.Count >= 2 && Calc(0, x) >= Calc(1, x)) lines.PopHead();
      |                ^~~~~
a.cc: In member function 'long int ConvexHullTrick_Monotone::Query_dec(long int)':
a.cc:94:16: error: 'lines' was not declared in this scope
   94 |         while (lines.Count >= 2 && Calc(lines.Count - 1, x) >= Calc(lines.Count - 2, x)) lines.PopTail();
      |                ^~~~~
a.cc:95:40: error: 'lines' was not declared in this scope
   95 |         return (isMin ? 1 : -1) * Calc(lines.Count - 1, x);
      |