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.

 
 
 
 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;
int n;
#define N 300009
#define ll long long
ll a[][N],sum[][N],s[N];
/*
sum[0][i]:从a[0][i]走到a[1][i]的花费,a[0][i]为起点,a[1][i]为终点
sum[0][i]:从a[1][i]走到a[0][i]的花费,a[1][i]为起点,a[0][i]为终点
s[i] : 前i列的两行数据之和
*/
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%lld",&a[][i]);
for(int i=;i<=n;i++) scanf("%lld",&a[][i]);
for(int i=;i<=n;i++) s[i]=s[i-]+a[][i]+a[][i];
sum[][n]=a[][n];sum[][n]=a[][n];
for(int i=n-;i>=;i--){
sum[][i]=sum[][i+]+s[n]-s[i]+a[][i]*(+*(n-i));
/*
s[n]-s[i]+a[1][i]*(1+2*(n-i)):sum[0][i]与sum[1][i]的差值
*/
sum[][i]=sum[][i+]+s[n]-s[i]+a[][i]*(+*(n-i));
}
ll ans=sum[][];
ll cnt=;
//只能前面若干个锯齿行走,在U 行走
for(int i=;i<=n;i++){
if(i&){
cnt+=a[][i]*(*i-)+a[][i]*(*i-);//锯齿花费
ans=max(ans,cnt+sum[][i+]+(s[n]-s[i])*(*i));
//(s[n]-s[i])*(2*i) :sum[1][i+1]的起点为i+1,但实际不是
}
else{
cnt+=a[][i]*(*i-)+a[][i]*(*i-);
ans=max(ans,cnt+sum[][i+]+(s[n]-s[i])*(*i));
}
}
printf("%lld\n",ans);
return ;
}

cf 1016C的更多相关文章

  1. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  2. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  3. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  4. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  5. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  6. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  7. CF memsql Start[c]UP 2.0 B

    CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...

  8. CF #376 (Div. 2) C. dfs

    1.CF #376 (Div. 2)    C. Socks       dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...

  9. CF #375 (Div. 2) D. bfs

    1.CF #375 (Div. 2)  D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...

随机推荐

  1. Jenkins+Gitlab+Ansible自动化部署(六)

    Pipeline Job实现Nginix+MySQL+PHP+Wordpress实现自动化部署交付(Jenkins+Gitlab+Ansible自动化部署(五)https://www.cnblogs. ...

  2. 使用MRUnit对MapReduce进行单元测试

    1. 为什么需要单元测试 一旦MapReduce项目提交到集群之后,若是出现问题是很难定位和修改的,只能通过打印日志的方式进行筛选.又如果数据和项目较大时,修改起来则更加麻烦.所以,在将MapRedu ...

  3. 如何在windows下是用mysqldumpslow命令

    1. 再一次点击mysql安装文件(默认是没安装mysqldumpslow这些脚本的),如图: 点击next如下图 点击Developer Components 旁边的选择this feature , ...

  4. 现阶段github上的emysql编译无法通过的问题

    最近在写db引擎,今天用到了emysql,找到https://github.com/Eonblast/Emysql,拽下来,然后发现竟然编译不通过~~去网上找了下资料,在 http://erlang. ...

  5. Java程序运行参数

    Java主函数形式:public static void main(String[] args){......} 也就是说可以向Java程序传递一个String[]. 1.在IDEA中debug.ru ...

  6. 从零开始的Lua宅[1]:编译Lua解释器

    Lua是一门神奇的脚本语言,游戏宅必备,懒人必备.Lua差多不是学起来用起来最简单的语言了,以至于简单到自身就是文档,自身就是配置文件.但是Lua的运行效率却是众多脚本中非常高的,据说仅次于V8爹下的 ...

  7. iOS 应用架构 (一)

    摘要:iOS 客户端应用架构看似简单,但实际上要考虑的事情不少.本文作者将以系列文章的形式来回答 iOS 应用架构中的种种问题,本文是其中的第一篇,主要讲架构设计的通识和方法论等,同时还讨论了大家关心 ...

  8. python(一)

    1 python安装 先安装python,之后安装pycharm 创建工程时需要关联解释器路径: 2 python运行 先编译后解释 .py--.pyc文件---解析----结果 .pyc的目的是减少 ...

  9. jmeter参考网址

    http://blog.csdn.net/dongdong9223/article/details/49248979 http://blog.csdn.net/hjh00/article/detail ...

  10. ABAP function group和Tomcat library重复加载问题

    ABAP ABAP help文档里对**LOAD-OF-PROGRAM"的关键字是这样描述的: This event keyword defines the program construc ...