
斜率优化dp,比较裸
注意int64的运算
var f,a,q:array[..] of int64;
i,n,h,t:longint;
x,y,z:int64; function g(j,k:int64):double;
var p,q:double;
begin
p:=*(f[k]-f[j])+sqr(k)-sqr(j)-k+j;
q:=*(k-j);
exit(p/q);
end; begin
readln(n);
for i:= to n do
read(a[i]);
h:=;
t:=;
q[]:=n;
f[n]:=a[n];
for i:=n- downto do
begin
while (h<t) and (g(q[h],q[h+])>i) do inc(h);
x:=q[h];
y:=i;
z:=(x-y)*(x-y-);
f[i]:=(*f[q[h]]+z) div +a[i];
if i<> then
begin
while (h<t) and (g(q[t],i)>g(q[t-],q[t])) do dec(t);
inc(t);
q[t]:=i;
end;
end;
writeln(f[]);
end.