#include using namespace std; int main(){ int64_t n; cin>>n; vector v(n); for (auto &i: v) cin>>i; int64_t const maxv=*max_element(v.begin(),v.end()); v.push_back(maxv); vector dp(maxv+1,n*n); vector prev(maxv+1,-1); for (int64_t i=0; i<=n; i++){ int64_t res=(i*i); for (int64_t j=0; j<=v[i]; j++){ if (prev[j]!=-1){ res=min(res,dp[j]+(i-prev[j]-1)*(i-prev[j]-1)); } } dp[v[i]]=res; prev[v[i]]=i; if (i==n){ cout<