#include using namespace std; using ll = long long; #define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i) #define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i) #define all(x) begin(x), end(x) template bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; } template bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; } template class ConvexHullTrick { public: void add(T a, T b) { a = -a, b = -b; auto m = lines.insert({a, b, 0}); auto l = m, r = m; ++r; while (update(m, r)) { r = lines.erase(r); } if (l != lines.begin() && update(--l, m)) { m = lines.erase(m); update(l, m); } m = l; while (l != lines.begin() && (--l)->p >= m->p) { update(l, lines.erase(m)); m = l; } } T get(T x) const { assert(!lines.empty()); auto it = *lines.lower_bound(x); return -(it.a * x + it.b); } private: struct Line { mutable T a, b; // ax + b mutable double p; // intersection point with the next line bool operator<(const Line& o) const { return a < o.a; } bool operator<(T x) const { return p < x; } }; using iterator = typename std::multiset>::iterator; static constexpr double INF = std::numeric_limits::max() / 2; std::multiset> lines; bool update(iterator x, iterator y) const { if (y == lines.end()) { x->p = INF; return false; } if (x->a == y->a) { x->p = (x->b > y->b ? INF : -INF); } else { x->p = 1.0 * (y->b - x->b) / (x->a - y->a); } return x->p >= y->p; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); int N; cin >> N; vector Q(N); for (auto& x : Q) cin >> x; vector dp(N); ConvexHullTrick cht; cht.add(-Q[0], 0); ll ans = 0; rep(i, 1, N) { dp[i] = -cht.get(Q[i]); cht.add(-Q[i], -max(0LL, dp[i])); chmax(ans, dp[i]); } cout << ans << endl; }