C. Vasya And The Mushrooms
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
inputCopy
3
1 2 3
6 5 4
outputCopy
70
inputCopy
3
1 1000 10000
10 100 100000
outputCopy
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.
解析:
这题感觉一点也不像C题,写了好长时间。。。
我们可以假定一直在上下循环往复的走,到达一个新的一列时,我们就要考虑从当前点一直走到头再回来获取的数字是多大(先要把这个值预处理出来,前缀和乱搞。。。),最后和一直循环往复的这个值比较一下就能得到最大结果
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define inf 200000099999999
#define mod 998244353
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep1(i,a,b) for(int i=a;i>=b;i--)
const int N=3e5+100;
ll ar[N],ap[N],n;
ll cos1[N],cos2[N];
ll vis1[N];
ll sum1[N],sum2[N],su1[N],su2[N];
void pre()
{
rep(i,1,n) vis1[i]=(i-1)*ar[i];
rep1(i,n,1) sum1[i]=sum1[i+1]+vis1[i],su1[i]=su1[i+1]+ar[i];
rep1(i,n,1) sum2[i]=(n+n-i)*ap[i]+sum2[i+1],su2[i]=su2[i+1]+ap[i];
rep(i,1,n) if(i%2) cos1[i]=sum1[i]+sum2[i]+(su1[i]+su2[i])*(i-1);
memset(sum1,0,sizeof sum1);
memset(sum2,0,sizeof sum2);
rep(i,2,n) vis1[i]=i*ap[i];
rep1(i,n,2) sum1[i]=sum1[i+1]+vis1[i],su1[i]=su1[i+1]+ap[i];
rep1(i,n,1) sum2[i]=(n+1+n-i)*ar[i]+sum2[i+1],su2[i]=su2[i+1]+ar[i];
rep(i,1,n) if(i%2==0) cos2[i]=(su1[i]+su2[i])*(i-2)+sum1[i]+sum2[i];
}
int main()
{
ios::sync_with_stdio(0);
#ifdef local
freopen("D://rush.txt","r",stdin);
#endif // local
cin>>n;
rep(i,1,n) cin>>ar[i];
rep(i,1,n) cin>>ap[i];
pre();
ll mx=0;
ll cnt=0,s=0;
for(int i=1;i<=n;i++)
{
if(i%2)
{
mx=max(mx,s+cos1[i]);
s+=ar[i]*cnt++;
s+=ap[i]*cnt++;
}
else
{
mx=max(mx,s+cos2[i]);
s+=ap[i]*cnt++;
s+=ar[i]*cnt++;
}
mx=max(mx,s);
}
cout<<mx<<endl;
return 0;
}