Codeforces 1016C 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

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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章