1. 程式人生 > >[codeforces 1016C]Vasya And The Mushrooms

[codeforces 1016C]Vasya And The Mushrooms

time limit per test :2 seconds memory limit per test :256 megabytes

asya’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(1n3105)n (1 ≤ n ≤ 3·10^5) — the length of the glade.

The second line contains nn numbers a1,a2,...,an(1ai106)a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6)

06) — the growth rate of mushrooms in the first row of the glade.

The third line contains nn numbers b1,b2,...,bn(1bi106)b_1, b_2, ..., b_n (1 ≤ b_i ≤ 10^6) 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.

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 01+12+23+34+45+56=70.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 01+110+2100+31000+410000+5100000=543210.0·1 + 1·10 + 2·100 + 3·1000 + 4·10000 + 5·100000 = 543210.

題意: 有一個2n2*n的蘑菇田,剛開始每個田裡的蘑菇的高度都是0,格子[i,j][i,j]處的蘑菇每過一秒都會增加a[i][j]a[i][j]的單位重量,問從[1,1][1,1]出發,不重複地採完蘑菇田,最多收穫多重的蘑菇?

題解: 要想能夠不重複地採完蘑菇田,走法必然是:先走一段曲折的,然後再走一個迴路:如下圖在這裡插入圖片描述 那麼我們只需要預處理出從第ii列開始走迴路的迴路上的答案;列舉從第ii列開始走迴路計算答案,之前的折路的答案可以在列舉的過程中算出,那麼這樣就可以知道最多能採多少了。

#include<bits/stdc++.h>
#define LiangJiaJun main
#define ll long long
using namespace std;
int n,a[2][300004];
ll sf[2][300004];
ll ca[2][300004];

int w33ha(){
    memset(sf,0,sizeof(sf));
    memset(ca,0,sizeof(ca));
    for(int t=0;t<=1;t++)
        for(int i=1;i<=n;i++)
            scanf("%d",&a[t][i]);
    for(int t=0;t<=1;t++){
        sf[t][0]=0;
        for(int i=1;i<=n;i++){
            sf[t][i]=sf[t][i-1]+a[t][i];
        }
    }
    for(int t=0;t<=1;t++){
        ca[t][n+1]=0;
        for(int i=n;i>=1;i--){
            ll nob=((n-i+1LL)<<1)-1;
            ca[t][i]=ca[t][i+1];
            ca[t][i]+=nob*a[1-t][i];
            ca[t][i]+=sf[t][n]-sf[t][i]+sf[1-t][n]-sf[1-t][i];
        }
    }
    ll ans=0,now=0;
    for(int i=1;i<=n;i++){
        ll delta=((i-1LL)<<1)*(sf[0][n]-sf[0][i-1]+sf[1][n]-sf[1][i-1]);
        if(i&1){
            ans=max(ans,now+delta+ca[0][i]);
            now+=((i-1LL)<<1)*a[0][i]+((i-1LL)<<1|1)*a[1][i];
        }
        else{
            ans=max(ans,now+delta+ca[1][i]);
            now+=((i-1LL)<<1)*a[1][i]+((i-1LL)<<1|1)*a[0][i];
        }
    }
    printf("%lld\n",ans);
    return 0;
}
int LiangJiaJun(){
    while(scanf("%d",&n)!=EOF)w33ha();
    return 0;
}