Codeforces 1016C Vasya And The Mushrooms

版权声明:转载请说明出处 https://blog.csdn.net/bao___zi/article/details/81604749

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的矩阵,每一个格子会有一个蘑菇的增长速率,每一天会增长一定的重量,一开始有一个人在左上角,他会把每一个格子都走一遍,当他走到一个格子的时候,他就会把当前位置的蘑菇全部都摘了,求他能摘取的最大重量。

解题思路:我们从题分析可以知道,他只有两种走法:

1.下右上右 这样循环走

2.先一直往右,然后掉头然后再一直往左。

这样的话我们可以从后开始枚举分界线,左边是第一种走法,然后右边是第二种走法。为什么左边不能是第二种走法尼,多花几个图就能看到。这种方法是不可能满足题意的。

之后我们只需要预处理一些前缀和就行。

#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=6e5+10;
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
ll n,m,a[maxn],b[maxn];
ll sum_pre[maxn],sum_s[maxn],sum_n[maxn],suml[maxn],sumr[maxn];
void fun(){
	int i,j;
	
	for(i=n;i>=1;i--) sum_pre[i]=sum_pre[i+1]+a[i]+b[i];
	for(i=1;i<=n;i++){
		sum_s[i]=sum_s[i-1]+(i-1)*a[i];
		sum_n[i]=sum_n[i-1]+(i-1)*b[i]; 
	}
	for(i=n;i>=1;i--){
		sum_s[2*n+1-i]=sum_s[2*n-i]+(2*n-i)*b[i];
		sum_n[2*n+1-i]=sum_n[2*n-i]+(2*n-i)*a[i];
	}
	for(i=1;i<=n;i++){
		if(i%2){
			sumr[i]=sum_s[2*n-i+1]-sum_s[i-1]+(i-1)*sum_pre[i];
			suml[i]=suml[i-1]+(2*i-3)*a[i-1]+(2*i-4)*b[i-1];
		}
		else{
			sumr[i]=sum_n[2*n-i+1]-sum_n[i-1]+(i-1)*sum_pre[i];
			suml[i]=suml[i-1]+(2*i-4)*a[i-1]+(2*i-3)*b[i-1];
		}
	}
}
int main(){
	int i,j;
	scanf("%I64d",&n);
	for(i=1;i<=n;i++){
		scanf("%I64d",&a[i]);
	}
	for(i=1;i<=n;i++){
		scanf("%I64d",&b[i]);
	}
	fun();
	ll ans=0;
	for(i=1;i<=n;i++){
		ans=max(ans,suml[i]+sumr[i]);
	}
	printf("%I64d\n",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/bao___zi/article/details/81604749