# 题目

## Problem Description

Zero has an old printer that doesn’t work well sometimes. As it is antique, he still like to use it to print articles. But it is too old to work for a long time and it will certainly wear and tear, so Zero use a cost to evaluate this degree.
One day Zero want to print an article which has $$N$$ words, and each word $$i$$ has a cost $$C_i$$ to be printed. Also, Zero know that print $$k$$ words in one line will cost
$$(\sum\limits_{k=1}^kC_i)^2+M$$
$$M$$ is a const number.
Now Zero want to know the minimum cost in order to arrange the article perfectly.

## Input

There are many test cases. For each test case, There are two numbers N and M in the first line (0 ≤ n ≤ 500000, 0 ≤ M ≤ 1000). Then, there are N numbers in the next 2 to N + 1 lines. Input are terminated by EOF.

## Output

A single number, meaning the mininum cost to print the article.

5 5
5
9
5
7
5

230

# 题解

$$dp[i]=dp[j]+M+(sum[i]-sum[j])^2$$

$$dp[j]-dp[k]+2\times sum[i]\times (sum[k]-sum[j])+sum[j]^2-sum[k]^2<0$$

\frac{(dp[j]+sum[j])^2-(dp[k]+sum[k])^2}{2\times (sum[j]-sum[k])}
\lt sum[i]

$$\frac{y_j-y_k}{x_j-x_k}\lt sum[i]$$

1，用一个单调队列来维护解集。
2，假设队列中从头到尾已经有元素a b c。那么当d要入队的时候，我们维护队列的上凸性质，即如果g[d,c]<g[c,b]，那么就将c点删除。直到找到g[d,x]>=g[x,y]为止，并将d点加入在该位置中。