CodeForces 1016C-Vasya And The Mushrooms(前后缀+dp)

版权声明:转载请告知博主并要注明出处嗷~ https://blog.csdn.net/Akatsuki__Itachi/article/details/81870380

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.

题意:有一个2*n的矩阵,每个矩阵有一个分数,主人公要从左上角出发,每次只能走具有相邻边的方格,且每个方格只能走一次。每到达一个方格,可以获得的分数为(步数*该方格的初始分数),现在要使最后得到的分数总之最大化。

初始步数为0,具体计算见题意Note

思路:若想使得最后的分数最大,必须要把每个方格走完。既然要走完每个方格,那么路线就变得很少了,且有规律可循。

如下图:

第一种方案一定不行,因为没法走完所有的方格,那么只剩下后两种。

后两种方案可划分为两部分,第一部分是左边蛇形部分,第二部分是右边直线部分。

那么再看左边蛇形部分,可以用一个前缀数组打个表,存一下和。

右边的直线部分可以分为两种情况:

位置下标从1开始。、

①当 方格的位置为奇数时,终点一定位于第二行。

②当方格的位置为偶数时,终点一定位于第一行。 

这样我们再用两个后缀数组,将终点在第一行的情况和终点在第二行的情况存储下来。

最后枚举O(N),用蛇形部分+直线部分记录最大值即可。

对于后缀数组,举个例子

4

1 2 3 4

8 7 6 5

设dp1[],dp2[]两个后缀数组,分别代表①②两种情况

那么dp1[4]=6*4+7*5,dp2[4]=6*5+7*4

dp1[3]=4*3+5*4+6*5+7*6,dp2[3]=4*6+5*5+6*4+7*3

....

这样其实我们只需要计算当前位置的数字即可,再用一个sum后缀数组存储后面的数的和。

如:sum[4]=4+5=9,sum[3]=3+4+5+6=15.

这样,如果我们要计算dp[3],那么dp[3]=dp[4]*sum[4]+4*3+7*6.

做完看网上其它的解法,代码好像很简单......我似乎绕了一些弯路,不管了,告辞!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
#define memset(a,v)  memset(a,v,sizeof(a))
using namespace std;
const int MAXL(3*1e5);
const int INF(0x3f3f3f3f);
const int mod(1e9+7);
typedef long long int LL;
int a[2][MAXL+50];
LL dp[MAXL+50];
LL dp1[MAXL+50];
LL dp2[MAXL+50];
LL sum[MAXL+50];
int n;
void init()
{
    dp[0]=0,dp1[n+1]=dp2[n+1]=0,sum[n+1]=0;
    for(LL i=1,num=0; i<=n; i++,num+=2)
    {
        if(i&1)
            dp[i]=dp[i-1]+(num*a[0][i]+(num+1)*a[1][i]);
        else
            dp[i]=dp[i-1]+(num*a[1][i]+(num+1)*a[0][i]);
    }
    for(LL i=n; i>=1; i--)
    {
        LL temp=(i-1)*2;
        LL num=(n-i)*2;
        sum[i]=sum[i+1]+a[0][i]+a[1][i];
        dp1[i]=dp1[i+1]-sum[i+1]+temp*a[0][i]+(temp+num+1)*a[1][i];
        dp2[i]=dp2[i+1]-sum[i+1]+temp*a[1][i]+(temp+num+1)*a[0][i];
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%d",&a[0][i]);
    for(int i=1; i<=n; i++)
        scanf("%d",&a[1][i]);
    init();
    LL ans=0;
    for(int i=0;i<=n;i++)
    {
        if(i&1)
            ans=max(ans,dp[i]+dp2[i+1]);
        else
            ans=max(ans,dp[i]+dp1[i+1]);
    }
    cout<<ans<<endl;
}

猜你喜欢

转载自blog.csdn.net/Akatsuki__Itachi/article/details/81870380