#include using namespace std; using lint = long long int; struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template istream &operator>>(istream &is, vector &vec){ for (auto &v : vec) is >> v; return is; } template ostream &operator<<(ostream &os, const vector &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template ostream &operator<<(ostream &os, const deque &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template ostream &operator<<(ostream &os, const pair &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; } // Convex Hull Trick for monotone increasing queries, monotone decreasing slopes // Each operation is amortized O(1) // - add_line(a, b): Add `y = ax + b`, a must be monotone decreasing // - add_convex_parabola(c, a, b): Add `y = c(x - a)^2 + b` (c is constant & a is monotone increasing) // - get(x): Calculate min/max. value of `y = ax + b`'s at point x, x must be monotone increasing // - parabola_get(c, x): Caclculate min/max. value of `y = c(x - a)^2 + b`'s template struct MonotoneConvexHullTrick : deque> // (a, b) means `y = ax + b` { using T_MP = __int128_t; bool is_minimizer; // If true, this instance calculates min. Otherwise, calculates max. MonotoneConvexHullTrick(bool is_minimizer) : is_minimizer(is_minimizer) {} void add_line(T_CHT a, T_CHT b) { // Add y = ax + b if (!is_minimizer) a = -a, b = -b; while (this->size() > 1u) { int sz = this->size(); __int128_t l = (__int128_t)(this->back().second - (*this)[sz - 2].second) * (this->back().first - a); // Overflow might occur here. __int128_t r = (__int128_t)(b - this->back().second) * ((*this)[sz - 2].first - this->back().first); if (l < r) break; this->pop_back(); } this->emplace_back(a, b); } T_CHT get(T_CHT x) { while (this->size() > 1u and (*this)[0].first * x + (*this)[0].second > (*this)[1].first * x + (*this)[1].second) { this->pop_front(); } return ((*this)[0].first * x + (*this)[0].second) * (is_minimizer ? 1 : -1); } void add_convex_parabola(T_CHT c, T_CHT a, T_CHT b) { add_line(-2 * c * a, c * a * a + b); } // Add y = c(x - a)^2 + b T_CHT parabola_get(T_CHT c, T_CHT x) { return get(x) + c * x * x; } }; int main() { int N; cin >> N; vector A(N); cin >> A; vector> cht(N + 1, true); int x = 0; cht[0].add_convex_parabola(1, x, 0); REP(i, N) { IREP(d, i + 1) { lint v = cht[d].parabola_get(1, x); cht[d + 1].add_convex_parabola(1, x + A[i], v); } x += A[i]; } IREP(d, N) { lint v = cht[d].parabola_get(1, x); cout << v << endl; } }