其实这题一看知道应该是DP,再一看数据范围肯定就是单调队列了。
不过我还不太懂神马单调队列、斜率优化……
附上天牛的题解:http://www.cnblogs.com/neverforget/archive/2012/04/19/2456483.html
var f,g:array[..] of int64;
s,q:array[..] of longint;
a,b,c,n,i,h,t,x:longint;
bestk:double;
procedure init;
begin
readln(n);
readln(a,b,c);
s[]:=;
for i:= to n do
begin
read(x);
s[i]:=s[i-]+x;
end;
end;
function k(x,y:longint):double;
begin
exit(double(g[y]-g[x])/(s[y]-s[x]));
end;
procedure main;
begin
f[]:=;h:=;t:=;q[]:=;
for i:= to n do
begin
bestk:=double(*a*s[i]);
while (h<t) and (k(q[h],q[h+])>=bestk) do inc(h);
f[i]:=int64(f[q[h]])+int64(a)*int64(s[i]-s[q[h]])*int64(s[i]-s[q[h]])
+int64(b)*int64(s[i]-s[q[h]])+int64(c);
g[i]:=int64(f[i])+int64(a)*int64(s[i])*int64(s[i])-int64(b)*int64(s[i]);
while (h+<=t) and (k(q[t],i)>k(q[t-],q[t])) do dec(t);
inc(t);
q[t]:=i;
end;
end;
procedure print;
begin
writeln(f[n]);
end;
begin
init;
main;
print;
end.