//By Don4ick
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
#define forn(i, n) for (int i = 1; i <= n; i++)
#define pb push_back
#define all(x) x.begin(), x.end()
#define y1 qwer1234
const double PI = acos(-1.0);
const int DIR = 4;
const int X[] = {1, 0, -1, 0};
const int Y[] = {0, 1, 0, -1};
const int N = (int)2e5 + 228;
const ll INF = (ll)1e18 + 228;
using namespace std;
int n, a[N], ptr[N << 2], l1[N], r1[N], p[N];
ll dp[N];
vector < pair < ll, ll > > t[N << 2];
double cross(pair < ll, ll > x, pair < ll, ll > y)
{
return 1.0 * (0.0 + y.second - x.second) / (0.0 + x.first - y.first);
}
void add(vector < pair < ll, ll > > & v, pair < ll, ll > pp)
{
while((int)v.size() >= 2 && cross(v[(int)v.size() - 2], v[(int)v.size() - 1]) > cross(v[(int)v.size() - 2], pp))
v.pop_back();
v.pb(pp);
}
void build(int l, int r, int v, int tl, int tr)
{
if (l > r || tl > r || tr < l)
return;
if (tl >= l && tr <= r)
{
ptr[v] = 0;
t[v].clear();
for (int i = tl; i <= tr; i++)
{
add(t[v], make_pair(-2ll * (i - 1), dp[i - 1] + 1ll * (i - 1) * (i - 1)));
}
}
if (tl == tr)
return;
int mid = (tl + tr) >> 1;
build(l, r, v << 1, tl, mid);
build(l, r, v << 1 | 1, mid + 1, tr);
}
ll get(int l, int r, int v, int tl, int tr, int x)
{
if (l > r || tl > r || tr < l)
return INF;
if (tl >= l && tr <= r)
{
while(ptr[v] < (int)t[v].size() - 1 && 0.0 + x > cross(t[v][ptr[v]], t[v][ptr[v] + 1]))
ptr[v]++;
if (ptr[v] < (int)t[v].size())
return t[v][ptr[v]].first * x + t[v][ptr[v]].second + 1ll * x * x;
return INF;
}
int mid = (tl + tr) >> 1;
return min(get(l, r, v << 1, tl, mid, x), get(l, r, v << 1 | 1, mid + 1, tr, x));
}
void relax(int l, int r, vector < int > v)
{
if (v.empty())
return;
vector < int > v1, v2;
vector < pair < ll, ll > > cht;
int mid = (l + r) >> 1;
for (auto it : v)
{
if (l1[it] <= l && r1[it] >= r)
{
add(cht, make_pair(-2ll * (it - 1), dp[it - 1] + 1ll * (it - 1) * (it - 1)));
}
else
{
if (l1[it] <= mid)
v1.pb(it);
if (r1[it] > mid)
v2.pb(it);
}
}
int pp = 0;
for (int i = l; i <= r; i++)
{
while(pp < (int)cht.size() - 1 && cross(cht[pp], cht[pp + 1]) < 0.0 + i)
pp++;
if (pp < (int)cht.size())
dp[i] = min(dp[i], cht[pp].first * i + cht[pp].second + 1ll * i * i);
}
if (l == r)
return;
relax(l, mid, v1);
relax(mid + 1, r, v2);
}
void calc(int l, int r)
{
if (l == r)
{
if (a[l] == 1)
dp[l] = min(dp[l], dp[l - 1] + 1);
return;
}
int mid = (l + r) >> 1;
// p[i] - first upper
calc(l, mid);
int mx1 = 0, mx2 = 0, ptr = mid + 1;
for (int i = mid + 1; i <= r; i++)
{
mx2 = max(mx2, a[i]);
while(ptr > l && max(mx1, a[ptr - 1]) <= mx2)
mx1 = max(mx1, a[--ptr]);
p[i] = ptr;
}
mx1 = mx2 = 0, ptr = mid;
for (int i = mid; i >= l; i--)
{
mx1 = max(mx1, a[i]);
while(ptr < r && max(mx2, a[ptr + 1]) <= mx1)
mx2 = max(mx2, a[++ptr]);
p[i] = ptr;
}
build(l, mid, 1, 1, n);
mx2 = 0;
for (int i = mid + 1; i <= r; i++)
{
mx2 = max(mx2, a[i]);
dp[i] = min(dp[i], get(p[i], i - mx2 + 1, 1, 1, n, i));
}
mx1 = 0;
vector < int > v;
for (int i = mid; i >= l; i--)
{
mx1 = max(mx1, a[i]);
l1[i] = i + mx1 - 1;
r1[i] = p[i];
v.pb(i);
}
reverse(all(v));
relax(mid + 1, r, v);
calc(mid + 1, r);
}
int main()
{
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
//cout.tie(NULL);
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
//~read
scanf("%d", &n);
forn(i,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