博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cf 1016C
阅读量:5225 次
发布时间:2019-06-14

本文共 3410 字,大约阅读时间需要 11 分钟。

C. Vasya And The Mushrooms
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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
input
Copy
3 1 2 3 6 5 4
output
Copy
70
input
Copy
3 1 1000 10000 10 100 100000
output
Copy
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.
 
 
 
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 typedef long long ll; 9 int n;10 #define N 30000911 #define ll long long 12 ll a[2][N],sum[2][N],s[N];13 /*14 sum[0][i]:从a[0][i]走到a[1][i]的花费,a[0][i]为起点,a[1][i]为终点15 sum[0][i]:从a[1][i]走到a[0][i]的花费,a[1][i]为起点,a[0][i]为终点16 s[i] : 前i列的两行数据之和17 */18 int main()19 {20 scanf("%d",&n);21 for(int i=1;i<=n;i++) scanf("%lld",&a[0][i]);22 for(int i=1;i<=n;i++) scanf("%lld",&a[1][i]);23 for(int i=1;i<=n;i++) s[i]=s[i-1]+a[0][i]+a[1][i];24 sum[0][n]=a[1][n];sum[1][n]=a[0][n];25 for(int i=n-1;i>=1;i--){26 sum[0][i]=sum[0][i+1]+s[n]-s[i]+a[1][i]*(1+2*(n-i));27 /*28 s[n]-s[i]+a[1][i]*(1+2*(n-i)):sum[0][i]与sum[1][i]的差值29 */30 sum[1][i]=sum[1][i+1]+s[n]-s[i]+a[0][i]*(1+2*(n-i));31 }32 ll ans=sum[0][1];33 ll cnt=0;34 //只能前面若干个锯齿行走,在U 行走35 for(int i=1;i<=n;i++){36 if(i&1){37 cnt+=a[0][i]*(2*i-2)+a[1][i]*(2*i-1);//锯齿花费38 ans=max(ans,cnt+sum[1][i+1]+(s[n]-s[i])*(2*i));39 //(s[n]-s[i])*(2*i) :sum[1][i+1]的起点为i+1,但实际不是40 }41 else{42 cnt+=a[0][i]*(2*i-1)+a[1][i]*(2*i-2);43 ans=max(ans,cnt+sum[0][i+1]+(s[n]-s[i])*(2*i));44 }45 }46 printf("%lld\n",ans);47 return 0;48 }

 

转载于:https://www.cnblogs.com/tingtin/p/9435184.html

你可能感兴趣的文章
vmware tools 的安装(Read-only file system 的解决)
查看>>
数列求和总结
查看>>
「Unity」委托 将方法作为参数传递
查看>>
Unity学习疑问记录之隐藏与显示物体
查看>>
设计模式-学习
查看>>
button标签点击实现数量加减
查看>>
重置GNOME-TERMINAL
查看>>
quartz 实现调度任务 SchedulerManager
查看>>
new jordans 9 Nets
查看>>
redis哨兵集群、docker入门
查看>>
[翻译][架构设计]The Clean Architecture
查看>>
状态压缩DP
查看>>
Shell从入门到精通进阶之四:流程控制
查看>>
腾讯QQ、新浪微博等知名社交网络图标素材
查看>>
正则表达式2
查看>>
Unity3D_(插件)小地图自刷新制作Minimap小地图
查看>>
为什么分布式一定要有Redis?
查看>>
hihoCoder 1233 : Boxes(盒子)
查看>>
HihoCoder 1328 BFS 搜索
查看>>
Day2-h和p标签
查看>>