#include using namespace std; using int64 = long long; // const int mod = 1e9 + 7; const int mod = 998244353; const int64 infll = (1LL << 62) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template< typename T1, typename T2 > ostream &operator<<(ostream &os, const pair< T1, T2 > &p) { os << p.first << " " << p.second; return os; } template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; } template< typename T > ostream &operator<<(ostream &os, const vector< T > &v) { for(int i = 0; i < (int) v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template< typename T > istream &operator>>(istream &is, vector< T > &v) { for(T &in : v) is >> in; return is; } template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template< typename T = int64 > vector< T > make_v(size_t a) { return vector< T >(a); } template< typename T, typename... Ts > auto make_v(size_t a, Ts... ts) { return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...)); } template< typename T, typename V > typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) { t = v; } template< typename T, typename V > typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) { for(auto &e : t) fill_v(e, v); } template< typename F > struct FixPoint : F { FixPoint(F &&f) : F(forward< F >(f)) {} template< typename... Args > decltype(auto) operator()(Args &&... args) const { return F::operator()(*this, forward< Args >(args)...); } }; template< typename F > inline decltype(auto) MFP(F &&f) { return FixPoint< F >{forward< F >(f)}; } /** * @brief Dynamic-Li-Chao-Tree * @docs docs/dynamic-li-chao-tree.md */ template< typename T, T x_low, T x_high, T id > struct DynamicLiChaoTree { struct Line { T a, b; Line(T a, T b) : a(a), b(b) {} inline T get(T x) const { return a * x + b; } }; struct Node { Line x; Node *l, *r; Node(const Line &x) : x{x}, l{nullptr}, r{nullptr} {} }; Node *root; DynamicLiChaoTree() : root{nullptr} {} Node *add_line(Node *t, Line &x, const T &l, const T &r, const T &x_l, const T &x_r) { if(!t) return new Node(x); T t_l = t->x.get(l), t_r = t->x.get(r); if(t_l <= x_l && t_r <= x_r) { return t; } else if(t_l >= x_l && t_r >= x_r) { t->x = x; return t; } else { T m = (l + r) / 2; if(m == r) --m; T t_m = t->x.get(m), x_m = x.get(m); if(t_m > x_m) { swap(t->x, x); if(x_l >= t_l) t->l = add_line(t->l, x, l, m, t_l, t_m); else t->r = add_line(t->r, x, m + 1, r, t_m + x.a, t_r); } else { if(t_l >= x_l) t->l = add_line(t->l, x, l, m, x_l, x_m); else t->r = add_line(t->r, x, m + 1, r, x_m + x.a, x_r); } return t; } } void add_line(const T &a, const T &b) { Line x(a, b); root = add_line(root, x, x_low, x_high, x.get(x_low), x.get(x_high)); } Node *add_segment(Node *t, Line &x, const T &a, const T &b, const T &l, const T &r, const T &x_l, const T &x_r) { if(r < a || b < l) return t; if(a <= l && r <= b) { Line y{x}; return add_line(t, y, l, r, x_l, x_r); } if(t) { T t_l = t->x.get(l), t_r = t->x.get(r); if(t_l <= x_l && t_r <= x_r) return t; } else { t = new Node(Line(0, id)); } T m = (l + r) / 2; if(m == r) --m; T x_m = x.get(m); t->l = add_segment(t->l, x, a, b, l, m, x_l, x_m); t->r = add_segment(t->r, x, a, b, m + 1, r, x_m + x.a, x_r); return t; } void add_segment(const T &l, const T &r, const T &a, const T &b) { Line x(a, b); root = add_segment(root, x, l, r - 1, x_low, x_high, x.get(x_low), x.get(x_high)); } T query(const Node *t, const T &l, const T &r, const T &x) const { if(!t) return id; if(l == r) return t->x.get(x); T m = (l + r) / 2; if(m == r) --m; if(x <= m) return min(t->x.get(x), query(t->l, l, m, x)); else return min(t->x.get(x), query(t->r, m + 1, r, x)); } T query(const T &x) const { return query(root, x_low, x_high, x); } }; int main() { int N, C; cin >> N >> C; vector< int64 > A(N), B(N); for(int i = 0; i < N; i++) { cin >> A[i] >> B[i]; } vector< int64 > dp(N + 1, infll); dp[0] = 0; vector< int64 > dp2(N + 1, infll); dp2[0] = 0; DynamicLiChaoTree< int64, 0LL, 1100000000000LL, infll > latte; DynamicLiChaoTree< int64, 0LL, 200000LL, infll > malta; auto set_j = [&](int64 j, int64 v) { v += j * j * C; v += j * C; chmin(dp2[j], v); }; auto get_j = [&](int64 j) { return malta.query(j) + j * j * C + j * C; }; for(int64 i = 1; i <= N; i++) { int64 ret = infll; if(chmin(dp[i], get_j(i))) { set_j(i, dp[i]); } for(int64 j = 0; j < i; j++) { chmin(ret, dp2[j] + -(2 * j * (A[i - 1] + i * C))); } ret += i * i * C; ret -= i * C; ret += i * A[i - 1] * 2; ret /= 2; ret += B[i - 1]; ret *= 2; ret += i * i * C; ret -= i * C; ret -= i * A[i - 1] * 2; malta.add_line(A[i - 1] * 2 - 2 * i * C, ret); /* for(int64 j = i; j <= N; j++) { if(chmin(dp[j], ret + j * (A[i - 1] * 2 - 2 * i * C) + j * j * C + j * C)) { set_j(j, dp[j]); } } */ } cout << dp[N] / 2 << "\n"; }