题目:http://codeforces.com/contest/1016/problem/C
转载:https://blog.csdn.net/qq_40160605/article/details/81432788
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 300005;
int n;
LL a[N];
LL b[N];
LL sum[N],suma[N],sumb[N],sumc[N];
//预处理三种情况
void init()
{
for(int i = n;i >= 1;--i)
sum[i] = sum[i + 1] + a[i] + b[i];
for(int i = n,j = n;i >= 1;--i,++j)
suma[i] = suma[i + 1] + (i - 1) * a[i] + j * b[i];
for(int i = n,j = n;i >= 2;--i,++j)
sumb[i] = sumb[i + 1] + i * b[i] + (j + 1) * a[i];
sumb[1] = sumb[2] + b[1];
int tot = 0;
for(int i = 1;i <= n;++i)
{
if(i % 2 == 0){
sumc[i] += sumc[i - 1];
sumc[i] += 1LL * tot * b[i];tot++;
sumc[i] += 1LL * tot * a[i];tot++;
}
else{
sumc[i] += sumc[i - 1];
sumc[i] += 1LL * tot * a[i]; tot++;
sumc[i] += 1LL * tot * b[i]; tot++;
}
}
}
int main()
{
while(~scanf("%d",&n))
{
memset(sum,0,sizeof(sum));
memset(suma,0,sizeof(suma));
memset(sumb,0,sizeof(sumb));
memset(sumc,0,sizeof(sumc));
for(int i = 1;i <= n;++i)
{
scanf("%d",&a[i]);
}
for(int i = 1;i <= n;++i)
{
scanf("%d",&b[i]);
}
init();
LL ans = 0;
for(int i = 0;i <= n;++i)
{
if(i % 2 == 0){
ans = max(ans,sumc[i] + suma[i + 1] + i * sum[i + 1]);
}
else{
ans = max(ans,sumc[i] + sumb[i + 1] + (i - 1) * sum[i + 1]);
}
}
printf("%lld\n",ans);
}
return 0;
}
/*
6
12 8 12 17 20 5
17 4 8 8 8 4
*/