#include using namespace std; #define LOCAL #pragma region Macros typedef long long ll; #define ALL(x) (x).begin(),(x).end() const long long MOD=1000000007; // const long long MOD=998244353; const int INF=1e9; const long long IINF=1e18; const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; const char dir[4]={'D','R','U','L'}; template istream &operator>>(istream &is,vector &v){ for (T &x:v) is >> x; return is; } template ostream &operator<<(ostream &os,const vector &v){ for (int i=0;i ostream &operator<<(ostream &os,const pair &p){ os << '(' << p.first << ',' << p.second << ')'; return os; } template ostream &operator<<(ostream &os,const map &m){ os << '{'; for (auto itr=m.begin();itr!=m.end();++itr){ os << '(' << itr->first << ',' << itr->second << ')'; if (++itr!=m.end()) os << ','; --itr; } os << '}'; return os; } template ostream &operator<<(ostream &os,const set &s){ os << '{'; for (auto itr=s.begin();itr!=s.end();++itr){ os << *itr; if (++itr!=s.end()) os << ','; --itr; } os << '}'; return os; } void debug_out(){cerr << '\n';} template void debug_out(Head&& head,Tail&&... tail){ cerr << head; if (sizeof...(Tail)>0) cerr << ", "; debug_out(move(tail)...); } #ifdef LOCAL #define debug(...) cerr << " ";\ cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n';\ cerr << " ";\ debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif template T gcd(T x,T y){return y!=0?gcd(y,x%y):x;} template T lcm(T x,T y){return x/gcd(x,y)*y;} template inline bool chmin(T1 &a,T2 b){ if (a>b){a=b; return true;} return false; } template inline bool chmax(T1 &a,T2 b){ if (a struct ConvexHullTrick{ struct Line{ T a,b; Line(T a,T b):a(a),b(b){} }; deque Lines; bool empty() const {return Lines.empty();} inline int sgn(T a){return a==0?0:(a<0?-1:1);} inline bool check(const Line &a,const Line &b,const Line &c){ if (b.b==a.b||c.b==b.b) return sgn(b.a-a.a)*sgn(c.b-b.b)>=sgn(c.a-b.a)*sgn(b.b-a.b); return (long double)(b.a-a.a)*sgn(c.b-b.b)/(long double)abs(b.b-a.b)>=(long double)(c.a-b.a)*sgn(b.b-a.b)/(long double)abs(c.b-b.b); } void add(T a,T b){ if (!isMin) a*=-1,b*=-1; Line l(a,b); if (empty()){ Lines.emplace_back(l); return; } if (Lines.front().a<=a){ if (Lines.front().a==a){ if (Lines.front().b<=b) return; Lines.pop_front(); } while(Lines.size()>=2&&check(l,Lines.front(),Lines[1])) Lines.pop_front(); Lines.emplace_front(l); } else { if (Lines.back().a==a){ if (Lines.back().b<=b) return; Lines.pop_back(); } while(Lines.size()>=2&&check(Lines[Lines.size()-2],Lines.back(),l)) Lines.pop_back(); Lines.emplace_back(l); } } inline T f(const Line &l,const T &x){ return l.a*x+l.b; } T query(T x){ int lb=-1,ub=Lines.size()-1; while(ub-lb>1){ int mid=(ub+lb)>>1; (f(Lines[mid],x)>=f(Lines[mid+1],x)?lb:ub)=mid; } return (isMin?f(Lines[ub],x):-f(Lines[ub],x)); } T query_monotone_inc(T x){ while(Lines.size()>=2&&f(Lines.front(),x)>=f(Lines[1],x)) Lines.pop_front(); return (isMin?f(Lines.front(),x):-f(Lines.front(),x)); } T query_monotone_dec(T x){ while(Lines.size()>=2&&f(Lines.back(),x)>=f(Lines[Lines.size()-2],x)) Lines.pop_back(); return (isMin?f(Lines.back(),x):-f(Lines.back(),x)); } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector A(N); for (int i=0;i> A[i]; vector sum(N+1,0); for (int i=0;i> CHT(N+2); vector> dp(N+1,vector(N+1,IINF)); dp[0][0]=0; CHT[0].add(0,0); for (int i=0;i