#P9200. 「GMOI R2-T3」粒子环游

    ID: 10014 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>数学线段树二分树状数组洛谷原创O2优化枚举洛谷月赛

「GMOI R2-T3」粒子环游

Background

Xiao Z, who loves "Ke science" (kexue), is doing a boring experiment.

Problem Description

In the laboratory, there is a circular track formed by nn connected experimental chambers. The ii-th chamber is connected clockwise to the (i+1)(i+1)-th chamber (in particular, the nn-th chamber is connected to the 11-st chamber). At the same time, there is a newly built chamber numbered n+1n+1 that will be inserted into this circular track. It can be inserted between any two chambers that were originally adjacent.

The ii-th chamber can transport a particle with charge amount QQ to its next chamber, and the energy cost of this process is Q×ci\vert Q \vert \times c_i. In addition, the ii-th chamber itself stores an amount eie_i of charge (the charge amount can be positive or negative). Due to the well-known law of conservation of charge, the charge stored in the (n+1)(n+1)-th chamber and the algebraic sum of the total charge stored in the first nn chambers add up to 00.

Xiao Z has a particle that originally has no charge. After the (n+1)(n+1)-th chamber is inserted into the track, he will choose any chamber (including chamber n+1n+1) as the starting point, put the particle into it, and make it travel clockwise for one full loop and return to the starting point driven by the chambers' energy. Every time the particle arrives at a chamber (including the starting point), the amount of charge it carries becomes the algebraic sum of its previous charge and the charge stored in that chamber.

Note: the charge amount is added with the chamber's stored charge first, and then the energy contribution is calculated.

Now, Xiao Z wants to know: among all ways of inserting the new chamber and choosing the starting point, what is the minimum energy required for the particle to complete one loop?

Input Format

The first line contains a positive integer nn, representing the number of chambers originally on the circular track.

The second line contains n+1n+1 non-negative integers, in order representing c1,c2,...,cn+1c_1,c_2,...,c_{n+1}.

The third line contains nn integers, in order representing e1,e2,...,ene_1,e_2,...,e_{n}.

Output Format

Output one line containing one number, representing the minimum energy required for the particle to travel one full loop.

3
1 3 2 2
3 -5 1
9
12
4 7 7 8 8 4 5 5 9 10 1 1 10 
0 -5 7 8 1 -1 -6 8 2 4 10 8 
509

Hint

Explanation for Sample 11: one optimal plan is to insert chamber 44 between chamber 33 and chamber 11, choose chamber 44 as the starting point, and the energy cost is $1\times 2\ +\ 4\times 1\ + \vert -1 \vert \times 3 \ +\ 0 \times 2 =9$.

This problem uses bundled Subtask testdata.

Subtask nn\le cic_i\le ei\vert e_i\vert Special Property Corresponding Tests Total Score
00 300300 100100 - 151\sim 5 1010
11 10310^3 A\bf A 676\sim 7 55
22 10410^4 - 8128\sim12 1515
33 10510^5 10510^5 B\bf B 131613\sim 16 1010
44 2.5×1052.5\times 10^5 - 172517\sim 25 6060

Special Property A\bf A: for all i[1,n+1](iZ)i\in[1,n+1](i\in \Z), ci=0c_i=0.

Special Property B\bf B: for all i[1,n+1](iZ)i\in[1,n+1](i\in \Z), ci=1c_i=1.

For 100%100\% of the data, 1n2.5×1051\le n\le 2.5\times 10^5, 0ci1050\le c_i\le 10^5, 0ei1050\le |e_i|\le 10^5.

It is guaranteed that the answer fits in the range of long long.

Translated by ChatGPT 5