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. spring的2种类型转换器

    spring有2种类型转换器,一种是propertyEditor,一种是Converter.虽然都是类型转换,但是还是有细微差别. 所以这里以一个例子的形式来分析一下这2种类型转换的使用场景和差别. ...

  2. 开启 PHP 错误提示配置步骤详解

    PHP编码出错不提示,这对于开发来说,是很不方便的.下面讲解如何开启错误提示步骤: 1. 打开php.ini文件. 以我的ubuntu为例,这个文件在: /etc/php5/apache2 目录下. ...

  3. Spring Boot Admin 监控中心

    Spring Boot Admin 监控中心 Spring Boot Admin用来收集微服务系统的健康状态.会话数量.并发数.服务资源.延迟等度量信息 服务端 建立spring-cloud-admi ...

  4. Mvc异常处理器

    using System; using System.Text; using EMS.Domains.Core; using System.Web.Mvc; using Json.Net; using ...

  5. 【Java】 Maven多模块项目上传到Sonar扫描问题合集

    上传到Soanr时,项目有单元测试数,但是覆盖率为0 修改pom.xml <plugin> <groupId>org.apache.maven.plugins</grou ...

  6. 实例练习——轮播图 & 全选/全不选

    1 实例1:轮播图 1)实质就是改变图片的src 2)把图片的路径用数组存起来 3) “下一张”的实现就是改变数组的下标,用一个变量i控制,每次点击下标加1.“上一张”的实现正好相反.注意“i的变化” ...

  7. CF1168A Increasing by Modulo

    思路: 首先得做个转化,如果某个解法最终分别对a[i](i = 1, 2, ..., n)做了b[i](i = 1, 2, ..., n)次加1再取余的运算,那么可以等价地构造出x次(x = max( ...

  8. zblog去除底部版权信息 “请勿修改或删除主题版权及作者信息”

    场景:使用了免费模板,但底部带作者版权.删除版权信息的代码后访问前台弹窗:请勿修改或删除主题版权及作者信息... 1. 删除版权信息代码 使用notepad++搜索功能,搜索版权信息:如ABC,找到相 ...

  9. 关于日志造成的频繁的IO

    记录日志可能消耗大量的IO [Q] 每次写入都是一个IO操作 即使是同一个文件 两次写入也要打开两次IO操作 [F] 设想有这样一个扩展  把php中要记录的日志 用文件名 和 内容的方式记录在内存中 ...

  10. 如何处理VirtualBox启动错误消息:The vboxdrv kernel module is not loaded

    我在启动minikube时,遇到如下错误消息: Starting local Kubernetes v1.10.0 cluster... Starting VM... E1010 03:27:37.9 ...