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 ( 1 ≤ n ≤ 3 ⋅ 1 0 5 ) n (1 ≤ n ≤ 3·10^5) n(1 ≤ n ≤ 3⋅105) — the length of the glade.
The second line contains n n n numbers a 1 , a 2 , . . . , a n ( 1 ≤ a i ≤ 1 0 6 ) a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^6) a1, a2, ..., an(1 ≤ ai ≤ 106) — the growth rate of mushrooms in the first row of the glade.
The third line contains n n n numbers b 1 , b 2 , . . . , b n ( 1 ≤ b i ≤ 1 0 6 ) b_1, b_2, ..., b_n (1 ≤ b_i ≤ 10^6) 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.
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.
0·1 + 1·2 + 2·3 + 3·4 + 4·5 + 5·6 = 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
0
⋅
1
+
1
⋅
10
+
2
⋅
100
+
3
⋅
1000
+
4
⋅
10000
+
5
⋅
100000
=
543210.
0·1 + 1·10 + 2·100 + 3·1000 + 4·10000 + 5·100000 = 543210.
0⋅1 + 1⋅10 + 2⋅100 + 3⋅1000 + 4⋅10000 + 5⋅100000 = 543210.
题意:
有一个
2
∗
n
2*n
2∗n的蘑菇田,刚开始每个田里的蘑菇的高度都是0,格子
[
i
,
j
]
[i,j]
[i,j]处的蘑菇每过一秒都会增加
a
[
i
]
[
j
]
a[i][j]
a[i][j]的单位重量,问从
[
1
,
1
]
[1,1]
[1,1]出发,不重复地采完蘑菇田,最多收获多重的蘑菇?
题解:
要想能够不重复地采完蘑菇田,走法必然是:先走一段曲折的,然后再走一个回路:如下图
那么我们只需要预处理出从第
i
i
i列开始走回路的回路上的答案;枚举从第
i
i
i列开始走回路计算答案,之前的折路的答案可以在枚举的过程中算出,那么这样就可以知道最多能采多少了。
#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;
}