1. 程式人生 > >字首和的處理入門————codeforces 1016c——Vasya And The Mushrooms

字首和的處理入門————codeforces 1016c——Vasya And The Mushrooms

題意:

Vasya's house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into n consecutive cells. For each cell Vasya knows how fast the mushrooms grow in this cell (more formally, how many grams of mushrooms grow in this cell each minute). Vasya spends exactly one minute to move to some adjacent cell. Vasya cannot leave the glade. Two cells are considered adjacent if they share a common side. When Vasya enters some cell, he instantly collects all the mushrooms growing there.

Vasya begins his journey in the left upper cell. Every minute Vasya must move to some adjacent cell, he cannot wait for the mushrooms to grow. He wants to visit all the cells exactly once and maximize the total weight of the collected mushrooms. Initially, all mushrooms have a weight of 0. Note that Vasya doesn't need to return to the starting cell.

Help Vasya! Calculate the maximum total weight of mushrooms he can collect.

Input

The first line contains the number n (1 ≤ n ≤ 3·105) — the length of the glade.

The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 106) — the growth rate of mushrooms in the first row of the glade.

The third line contains n

 numbers b1, b2, ..., bn (1 ≤ bi ≤ 106) is the growth rate of mushrooms in the second row of the glade.

Output

Output one number — the maximum total weight of mushrooms that Vasya can collect by choosing the optimal route. Pay attention that Vasya must visit every cell of the glade exactly once.

Examples

Input

3
1 2 3
6 5 4

Output

70

Input

3
1 1000 10000
10 100 100000

Output

543210

Note

In the first test case, the optimal route is as follows:

Thus, the collected weight of mushrooms will be 0·1 + 1·2 + 2·3 + 3·4 + 4·5 + 5·6 = 70.

In the second test case, the optimal route is as follows:

Thus, the collected weight of mushrooms will be 0·1 + 1·10 + 2·100 + 3·1000 + 4·10000 + 5·100000 = 543210.

思路分析:對於這個題來說最後的終點位置的分佈是一定的,對於終點為奇數的位於第二排,為偶數的位於第一排,且成對頂分佈

於是可以得出

於是可以看出對於奇數定點來說,他得左半邊是一個曲折的圖形,而右半邊則是一望無際的大草原,那麼就牽扯到了本題的核心,字首和的預處理。題本身不難,難的是如何對字首和進行預處理,直接上程式碼;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;

typedef long long ll;
const int maxn=6e5+10;
ll sum[maxn];//用來記錄第一排和第二排在i位置的和,作用是方便下面處理終點右半邊和,先記住就好。
ll shun[maxn];//說過的,如果這個定點位於第二排,那麼他的右半邊一定是順時針的形狀,用來記錄順時針的累加值
ll ni[maxn];//同理記錄終點位於第一排時,右半邊為逆時針時的和;
ll lsum[maxn];//左半邊的值
ll rsum[maxn];//右半邊的值
ll r1[maxn],r2[maxn];//分別記錄第一排和第二排的值

int n;

void  ac()
{
    for(int i=n;i>=1;i--) sum[i]=sum[i+1]+r1[i]+r2[i];//在計算右半邊值時因為要第i個位置的lsum減去第i-1個位置的lsum後,通過這個陣列把值補全
    for(int i=1;i<=n;i++) shun[i]=shun[i-1]+r1[i]*(i-1),ni[i]=ni[i-1]+r2[i]*(i-1);先記錄第一排順時針和逆時針的累加和,
    for(int i=n;i>=1;i--) shun[2*n-i+1]=shun[2*n-i]+r2[i]*(2*n-i),ni[2*n-i+1]=ni[2*n-i]+r1[i]*(2*n-i);//記錄第二排的順時針和逆時針的累加和
    for(int i=1;i<=n;i++)
    {
        if(i%2)
        {
            rsum[i]=shun[2*n-i+1]-shun[i-1]+(i-1)*sum[i];//現在體現出了sum陣列的作用,因為之前計算shun和ni時每個值都是按照自己的順序去乘1,2,3.。。而實際上並不是,所以差的那些就通過sum陣列來補
            lsum[i]=lsum[i-1]+r1[i-1]*(2*i-3)+r2[i-1]*(2*i-4);
        }
        else
        {
            rsum[i]=ni[2*n-i+1]-ni[i-1]+(i-1)*sum[i];
            lsum[i]=lsum[i-1]+r1[i-1]*(2*i-4)+r2[i-1]*(2*i-3);
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>r1[i];
    for(int i=1;i<=n;i++) cin>>r2[i];
    ac();
    ll res=0;
    for(int i=1;i<=n;i++)
    {
        res=max(res,lsum[i]+rsum[i]);
    }
    cout<<res<<endl;
    return  0;
}