C. 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

Copy

3
1 2 3
6 5 4

Output

Copy

70

Input

Copy

3
1 1000 10000
10 100 100000

Output

Copy

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<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define per(i,a,b) for(ll i=b-1;i>=a;i--)

const int maxn=3e5+10;
ll sum[maxn*2],rev_sum[maxn*2];
ll s1[maxn*2],s2[maxn*2];


/*
写模拟题很慢,自己感觉有几个原因

//Attention !!!
1.状态定义不清楚。
 sum[i]的意思,这个式子的意思,感觉总是敲完,在debug的时候,看看怎样好改,怎样来做,前期不注意
 这些个细节,导致到最后的时候,留下很多的漏洞,脑子一遍一遍的筛,慢慢的才知道自己写的是什么
 但是,我们应该是先知道写的是什么,然后,我们再去写代码,不要总是模模糊糊的就过去了
 这样很难debug
2.要去找循环节,这样才能用程序跑
3.要自信
   要相信自己一定能写出来,不要总是怀疑自己,cf差不多500或者600人能做出来的题,我们就应该做出来
   
*/

ll v1[maxn],v2[maxn];
int main(){
    ll n;
    scanf("%lld",&n);
    rep(i,1,n+1){
        scanf("%lld",&v1[i]);
        sum[i]=sum[i-1]+i*v1[i];
        s1[i]=s1[i-1]+v1[i];
    }
    rep(i,1,n+1){
        scanf("%lld",&v2[i]);
        rev_sum[i]=rev_sum[i-1]+i*v2[i];
        s2[i]=s2[i-1]+v2[i];
    }
    per(i,1,n+1){
        int dif=n-i+1;
        sum[n+dif]=sum[n+dif-1]+(n+dif)*v2[i];
        rev_sum[n+dif]=rev_sum[n+dif-1]+(n+dif)*v1[i];
        s1[n+dif]=s1[n+dif-1]+v2[i];
        s2[n+dif]=s2[n+dif-1]+v1[i];
    }
    ll ans=sum[n+n]-sum[1]-(s1[n+n]-s1[1]);
    //printf("ans:%lld sum2n:%lld\n",ans,sum[2*n]);
    ll ss=0,tmp=0;
/*
3
1 2 3
6 5 4
*/
    ll add=0;
    for(int i=1;i<=n;i++){
        if(i&1){
            tmp=ss;
            tmp=tmp+sum[n+n-(i-1)]-sum[i-1]+(add-i)*(s1[n+n-(i-1)]-s1[i-1]);
            ans=max(ans,tmp);

            ss+=add*v1[i];
            add++;

            tmp=ss;
            tmp=tmp+rev_sum[n+n-i]-rev_sum[i-1]+(add-i)*(s2[n+n-i]-s2[i-1]);
            ans=max(ans,tmp);

            ss+=add*v2[i];
            add++;
           // printf("i:%d add:%lld ss:%lld\n",i,add,ss);
        }
        else{
            ss+=add*v2[i];
            add++;
            ss+=add*v1[i];
            add++;
            ans=max(ans,ss);
        }
    }
    printf("%lld\n",ans);
    return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值