#P4655. [CEOI 2017] Building Bridges

    ID: 3591 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dp2017线段树cdq 分治CEOI斜率优化

[CEOI 2017] Building Bridges

Description

There are nn pillars arranged in order, and each pillar has a height. The height of the ii-th pillar is hih_i.

Now we want to build several bridges. If a bridge is built between the ii-th pillar and the jj-th pillar, it costs (hihj)2(h_i-h_j)^2.

Before building bridges, all pillars that are not used will be demolished, because they would interfere with the construction process. The cost to demolish the ii-th pillar is wiw_i. Note that wiw_i is not necessarily non-negative, because the government may want some pillars to be demolished.

Now the government wants to know the minimum total cost to connect pillar 11 and pillar nn using bridges. Note that bridges cannot intersect anywhere other than at their endpoints.

Input Format

The first line contains a positive integer nn.

The second line contains nn space-separated integers, which are h1,h2,,hnh_1,h_2,\cdots,h_n in order.

The third line contains nn space-separated integers, which are w1,w2,,wnw_1,w_2,\cdots,w_n in order.

Output Format

Output one line with one integer, the minimum total cost. Note that the minimum total cost is not necessarily positive.

6
3 8 7 1 6 6
0 -1 9 1 2 0
17

Hint

For 100%100\% of the testdata, 2n105;0hi,wi1062\le n\le 10^5;0\le h_i,\vert w_i\vert\le 10^6.

Translated by ChatGPT 5