#include using namespace std; #include using namespace atcoder; using ll = long long; ll n,A,B,w; vector d; /** * @brief Convex Hull Trick Add Monotone * @docs docs/convex-hull-trick-add-monotone.md */ template< typename T, bool isMin > struct ConvexHullTrickAddMonotone {/*{{{*/ #define F first #define S second using P = pair< T, T >; deque< P > H; ConvexHullTrickAddMonotone() = default; bool empty() const { return H.empty(); } void clear() { H.clear(); } inline int sgn(T x) { return x == 0 ? 0 : (x < 0 ? -1 : 1); } inline bool check(const P &a, const P &b, const P &c) { if(b.S == a.S || c.S == b.S) return sgn(b.F - a.F) * sgn(c.S - b.S) >= sgn(c.F - b.F) * sgn(b.S - a.S); //return (b.F-a.F)*(c.S-b.S) >= (b.S-a.S)*(c.F-b.F); if(is_integral< T >::value) { return (b.S - a.S) / (a.F - b.F) >= (c.S - b.S) / (b.F - c.F); } else { return (b.F - a.F) * sgn(c.S - b.S) / abs(b.S - a.S) >= (c.F - b.F) * sgn(b.S - a.S) / abs(c.S - b.S); } } void add(T a, T b) { if(!isMin) a *= -1, b *= -1; P line(a, b); if(empty()) { H.emplace_front(line); return; } if(H.front().F <= a) { if(H.front().F == a) { if(H.front().S <= b) return; H.pop_front(); } while(H.size() >= 2 && check(line, H.front(), H[1])) H.pop_front(); H.emplace_front(line); } else { assert(a <= H.back().F); if(H.back().F == a) { if(H.back().S <= b) return; H.pop_back(); } while(H.size() >= 2 && check(H[H.size() - 2], H.back(), line)) H.pop_back(); H.emplace_back(line); } } inline T get_y(const P &a, const T &x) { return a.F * x + a.S; } T query(T x) { assert(!empty()); int l = -1, r = H.size() - 1; while(l + 1 < r) { int m = (l + r) >> 1; if(get_y(H[m], x) >= get_y(H[m + 1], x)) l = m; else r = m; } if(isMin) return get_y(H[r], x); return -get_y(H[r], x); } T query_monotone_inc(T x) { assert(!empty()); while(H.size() >= 2 && get_y(H.front(), x) >= get_y(H[1], x)) H.pop_front(); if(isMin) return get_y(H.front(), x); return -get_y(H.front(), x); } T query_monotone_dec(T x) { assert(!empty()); while(H.size() >= 2 && get_y(H.back(), x) >= get_y(H[H.size() - 2], x)) H.pop_back(); if(isMin) return get_y(H.back(), x); return -get_y(H.back(), x); } #undef F #undef S };/*}}}*/ void solve(){ vector dp(n+1,INT_MAX); ConvexHullTrickAddMonotone cht; dp[0] = w; for(int i = 0;i<=n;i++){ //if(i==1)continue; if(i){ i--; ll x = (i*i+i)/2*B-A*i; ll cur = cht.query(i)+x; i++; //if(i==1)cout<> n >> A >> B >> w; d = vector(n); for(auto &i:d)cin >> i; solve(); }