Building Bridges

Pick a subset containing the first and last pillars, pay (h_i-h_j)^2 for each bridge section and w_i for each skipped pillar, and minimize the total.

Hard8Dynamic programmingGreedyGeometryNo attempts yetTime limit3sMemory limit128 MB

Problem

A wide river has nn pillars standing out of the water, and their heights may differ. The pillars stand in a straight line from one bank to the other. You want to build a bridge that rests on these pillars. To do that, pick a subset of the pillars and join the tops of consecutive picked pillars into bridge sections. The subset must contain the first pillar and the last pillar.

Building a section between two consecutive picked pillars ii and jj costs (hihj)2(h_i - h_j)^2, where hih_i is the height of pillar ii. The square keeps the bridge from running steeply uphill or downhill. Every pillar left out of the bridge blocks river traffic and has to be removed. Removing pillar ii costs wiw_i. This cost can be negative, because some interested parties are willing to pay you to get certain pillars out of the way. All heights hih_i and all costs wiw_i are integers.

Find the minimum possible total cost of a bridge that connects the first pillar to the last one.

Input

The first line contains the number of pillars nn. The second line contains the heights hih_i in order, separated by spaces. The third line contains the removal costs wiw_i in the same order.

Output

Print the minimum cost of building the bridge. The value can be negative.

Constraints

  • 2n1052 \le n \le 10^5
  • 0hi1060 \le h_i \le 10^6
  • wi106|w_i| \le 10^6