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. Java基础:(五)Object通用方法

    一.Object对象的九个方法 getClass():hashCode():equals():clone():toString():notify():notifyAll():wait():finali ...

  2. Log4j输出格式log4j的PatternLayout参数含义

    摘自:http://logging.apache.org/log4j/docs/api/org/apache/log4j/PatternLayout.html 参数 说明 例子 %c 列出logger ...

  3. C++ 中函数后面跟const是什么意思

    问题:c++:void display( ) const 中的const是什么意思?简答:意思是除了表明了mutable的成员变量以外该类的其他的成员变量在这个函数内一律不能修改. 详细:加const ...

  4. Android Studio maven-metadata.xml 卡着不动原因和解决方法

    头一天好好的,第二天就卡着了. 一直在这个地方不动,如果停止就会报 Error:Could not run build action using Gradle distribution ‘https: ...

  5. Eclipse IDE配置PHP开发、调试环境

    前言 使用java语言开发的朋友想必对Eclipse开发工具已经不陌生了,那么Eclipse作为java主流的开发工具,是否能够开发PHP项目呢?答案如你所想,肯定是可以的!以下就是该IDE下如何配置 ...

  6. Memcache笔记02-telnet操作memcached

    telnet操作Memcached 登录到telnet连接到memcached服务: telnet 127.0.0.1 11211 memcached的基本命令: //当telnet登录成功可以看到一 ...

  7. 编写生成32位大写和小写字符的md5的函数

    package nicetime.com.practise; import java.security.MessageDigest; /** * MD5加密是JAVA应用中常见的算法,请写出两个MD5 ...

  8. POJ 1067 取石子游戏 (威佐夫博奕,公式)

    题意: 有两堆石子,两个人轮流取石子.规定每次有两种取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后把石子全部取完者为胜者.给定两堆石子数量,问先手的输赢? ...

  9. 2018.4.22 深入理解Java的接口和抽象类

    前言 对于面向对象编程来说,抽象是他的一大特征之一.在Java中,可以通过两种形式来体现oop 的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初雪的时候会以为他们可以随意互 ...

  10. 2018.4.16 Java多线程实现龟兔赛跑

    龟兔赛跑(通过多线程来实现 里面的具体方法) TT.java package com.lanqiao.demo3; /** * 乌龟 * @author Administrator * */ publ ...