一.考虑如下DP方程
1.m为常数,s表示数组前缀和
![dp[i]=min(dp[j]+(s[j]-s[i]])^2)+m](https://img.pic99.top/cnyincai/202505/f6ec7d2e41309c9.gif)
2.化简可得![dp[i]=dp[j]+s[j]^2+s[i]^2-2s[i]s[j]+m](https://img.pic99.top/cnyincai/202505/c709a65a901bca7.gif)
3.观察式子可知,若想dp[i]更小,则需要
更小
4.因此dp方程转换为![dp[j]=s[j]^2=dp[i]-s[i]^2+2s[i]s[j]+m](https://img.pic99.top/cnyincai/202505/86e5391141a95d.gif)
5.令y等于
,k等于
,x等于![dp[i]+s[i]^2+m](https://img.pic99.top/cnyincai/202505/1c09d5f71ebd.gif)
6.又因为
递增,因此用一个单调队列维护凸包即可
二.出队优化
1.队首出队
若存在两个点a,b,且a
若i从b转移优于从a转移,则有 ![dp[b]+(s[i]-s[b]^2)+m<=dp[a]+(s[i]-s[a])^2+m](https://img.pic99.top/cnyincai/202505/3dbc9c28ce2f9bd.gif)
化简式子后![(dp[b]+s[b]^2)-(dp[a]+s[a]^2)<=2s[i](s[b]-s[a])](https://img.pic99.top/cnyincai/202505/df0431770544971.gif)
又因为
,因此移项可得
时从队首弹出
2.队尾出队
考虑下方情况,因为斜率递增,因此答案取t1一定不优

因此要去掉t1,如果t1和t2斜率大于t1和i的斜率那么t1出队
![Yt1=dp[t1]+s[t1]^2\;\;\;\;Xt1=s[t1]](https://img.pic99.top/cnyincai/202505/11351a81a43c70.gif)
![Yt2=dp[t2]+s[t2]^2\;\;\;\;Xt2=s[t2]](https://img.pic99.top/cnyincai/202505/0ca107c1b989118.gif)
![Yi = dp[i]+s[i]^2\;\;\;\;Xi=s[i]](https://img.pic99.top/cnyincai/202505/2e885e25ddedef2.gif)
则
若k1>=k2,则弹出t1,式子化简为
