前缀和的处理入门————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;
}

猜你喜欢

转载自blog.csdn.net/qq_41670466/article/details/81590548