Educational Codeforces Round 48 (Rated for Div. 2) C题

C. Vasya And The Mushrooms

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

模拟类题型

AC代码

#include <iostream>

using namespace std;
#define N 300003
using ll=long long;
long long a[N];
long long b[N];
long long sum[N];
long long px[N];
long long qx[N];

int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;++i)
        cin>>a[i];
    for(int i=1;i<=n;++i)
        cin>>b[i];
    for(int i=n;i>=1;--i)
    {
        sum[i]=sum[i+1]+a[i]+b[i];
        px[i]=px[i+1]+sum[i+1]+a[i]+2ll*(n-i+1)*b[i];
        qx[i]=qx[i+1]+sum[i+1]+b[i]+2ll*(n-i+1)*a[i];
    }
    long long h;
    long long ans=0;
    int t;
    int i;
    t=0;for(i=2;i<=n;i++) {t++;ans+=t*a[i];}for(i=n;i;i--) {t++;ans+=t*b[i];}h=b[1];

    for(int i=2;i<=n;++i)
    {
        ///*
        if(i&1)
        {
            ans=max(ans,h+px[i]+(2*i-3)*sum[i]);
            h+=(2*i-2)*a[i]+(2*i-1)*b[i];

        }
        else
        {
            ans=max(ans,h+qx[i]+(2*i-3)*sum[i]);
            h+=(2*i-2)*b[i]+(2*i-1)*a[i];
        }
        //*/
        /*
        if(i&1) {ans=max(ans,h+px[i]+(i*2-3)*sum[i]);h+=(i*2-1)*b[i]+(i*2-2)*a[i];}
                else {ans=max(ans,h+qx[i]+(i*2-3)*sum[i]);h+=(i*2-2)*b[i]+(i*2-1)*a[i];}
        */
    }
    cout<<ans<<endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36921652/article/details/81408193