Laurenty and Shop

CodeForces - 586B

A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.

The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.

The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.

Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.

The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 ≤ i ≤ 2, 1 ≤ j ≤ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 ≤ j ≤ n). The city doesn't have any other crossings.

The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.

 Figure to the first sample.

Help Laurenty determine the minimum total time he needs to wait at the crossroads.

Input

The first line of the input contains integer n (2 ≤ n ≤ 50) — the number of houses in each row.

Each of the next two lines contains n - 1 space-separated integer — values aij (1 ≤ aij ≤ 100).

The last line contains n space-separated integers bj (1 ≤ bj ≤ 100).

Output

Print a single integer — the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.

Examples

Input
4
1 2 3
3 2 1
3 2 2 3
Output
12
Input
3
1 2
3 3
2 1 3
Output
11
Input
2
1
1
1 1
Output
4

Note

The first sample is shown on the figure above.

In the second sample, Laurenty's path can look as follows:

  • Laurenty crosses the avenue, the waiting time is 3;
  • Laurenty uses the second crossing in the first row, the waiting time is 2;
  • Laurenty uses the first crossing in the first row, the waiting time is 1;
  • Laurenty uses the first crossing in the first row, the waiting time is 1;
  • Laurenty crosses the avenue, the waiting time is 1;
  • Laurenty uses the second crossing in the second row, the waiting time is 3.

In total we get that the answer equals 11.

In the last sample Laurenty visits all the crossings, so the answer is 4.

sol:只有两行根本用不着写最短路,弄一个前缀和和一个后缀和扫两遍,找到最短路和次短路即可

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=,inf=0x3f3f3f3f;
int n,Dis[][N],Time[N];
int Qzh[][N],Hzh[][N];
int main()
{
int i,j,ans=;
R(n);
for(i=;i<=;i++)
{
for(j=;j<=n;j++) R(Dis[i][j]);
}
for(i=;i<=;i++)
{
Qzh[i][]=; for(j=;j<=n;j++) Qzh[i][j]=Qzh[i][j-]+Dis[i][j];
Hzh[i][n]=; for(j=n-;j>=;j--) Hzh[i][j]=Hzh[i][j+]+Dis[i][j+];
}
for(i=;i<=n;i++) R(Time[i]);
int Zuix=-,Cix=-;
for(i=;i<=n;i++) if(Zuix==-||Hzh[][i]+Time[i]+Qzh[][i]<Hzh[][Zuix]+Time[Zuix]+Qzh[][Zuix])
{
Zuix=i;
}
ans+=Hzh[][Zuix]+Time[Zuix]+Qzh[][Zuix];
for(i=;i<=n;i++) if(Cix==-||Qzh[][i]+Time[i]+Hzh[][i]<Qzh[][Cix]+Time[Cix]+Hzh[][Cix])
{
if(i==Zuix) continue;
Cix=i;
}
ans+=Qzh[][Cix]+Time[Cix]+Hzh[][Cix];
Wl(ans);
return ;
}
/*
input
4
1 2 3
3 2 1
3 2 2 3
output
12 input
3
1 2
3 3
2 1 3
output
11 input
2
1
1
1 1
output
4
*/

codeforces586B的更多相关文章

随机推荐

  1. Echo团队Alpha冲刺随笔 - 第六天

    项目冲刺情况 进展 开始着手服务器部署.小程序改了几个BUG,WEB端完成接近一半,后端主体功能大致完成 问题 服务器反向代理有点问题 心得 Learning By Doing! 今日会议内容 黄少勇 ...

  2. STM32固件库详解

    STM32固件库详解   emouse原创文章,转载请注明出处http://www.cnblogs.com/emouse/ 应部分网友要求,最新加入固件库以及开发环境使用入门视频教程,同时提供例程模板 ...

  3. Android学习之基础知识九—数据存储(持久化技术)

    数据持久化是将那些内存中的瞬时数据保存到存储设备,保证即使在手机或电脑关机的情况下,这些数据仍然不会丢失. Android系统中主要提供了3种方式用于简单地实现数据持久化功能:文件存储.SharedP ...

  4. java 变量及数据类型、原码、反码、补码

    Java基础——变量及数据类型 变量的概念 内存中的一个存储区域 变量名+数据类型 可在同一类型范围内不断变化 为什么定义变量: 用于不断的存放同一类型的常量,并可以重复使用 使用变量注意: 变量的作 ...

  5. 源码篇:Python 实战案例----银行系统

    import time import random import pickle import os class Card(object): def __init__(self, cardId, car ...

  6. odoo仓库单据产品过滤写法

    def onchange_picking_type_id(self, cr, uid,ids, picking_type_id, context=None): if picking_type_id i ...

  7. HSF源码阅读

    HSF各组成之间的关系 1 服务提供者注册与发布 <bean id="hsfTestService" class="com.test.service.impl.Hs ...

  8. mac安装CocoaPods遇到的问题及解决办法

    (1)sudo gem install cocoapods Fetching: i18n-0.7.0.gem (100%) Successfully installed i18n-0.7.0 Fetc ...

  9. Python高阶函数--map

    map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把list 的每个元素依次作用在函数 f 上,得到一个新的 list 并返回. 例如,对于lis ...

  10. Opencv 2.4.10 +VS2010 项目配置

    资料来源:http://blog.csdn.net/scottly1/article/details/40978625