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. 【Codeforces 1106E】 Lunar New Year and Red Envelopes

    Codeforces 1106 E 题意:有\(k\)个红包,第\(i\)个红包可以在\(s_i\)到\(t_i\)的时间内抢,同时获得\(w_i\)的钱,但是抢完以后一直到\(d_i\)都不可以继续 ...

  2. ORA-14551: 无法在查询中执行 DML 操作

    编写了一个oracle函数,函数体内实现一系列数据库的逻辑处理,涉及到数据的增删等操作,返回NCLOB类型. 然后通过查询方式调用函数: SELECT PKG.MY_FUN('A') FROM DUA ...

  3. C++模板的特化

    C++类模板的三种特化,讲得比较全面 By SmartPtr(http://www.cppblog.com/SmartPtr/) 针对一个模板参数的类模板特化的几种类型, 一是特化为绝对类型(全特化) ...

  4. Struts学习总结-02 上传文件

    Struts 2框架提供了内置支持处理文件上传使用基于HTML表单的文件上传.上传一个文件时,它通常会被存储在一个临时目录中,他们应该由Action类进行处理或移动到一个永久的目录,以确保数据不丢失. ...

  5. CF908D New Year and Arbitrary Arrangement 期望、DP

    题目传送门 题意:给出正整数$pa,pb,k$,最开始你有一个空串,每一次你有$\frac{pa}{pa + pb}$的概率向串最后放一个$a$,有$\frac{pb}{pa + pb}$的概率向串最 ...

  6. LinqPad的变量比较功能

    LinqPad是一个非常方便的C#工具(有免费版和收费版). 今天发现它的变量比较功能真是方便啊.且看3行代码产生如下结果: 说明:图中两个变量的成员属性值分别用红色和绿色背景标注:图很长,只截取了一 ...

  7. WPF中DataGrid中的DataGridCheckBoxColumn用法(全选,全否,反选)

    原文:WPF中DataGrid中的DataGridCheckBoxColumn用法(全选,全否,反选) 前台代码 <DataGrid.Columns> <DataGridCheckB ...

  8. Luogu P1477 [NOI2008]假面舞会

    一道非常神奇的图论题解法无比新奇清新 我们首先把图分成三种情况: 有环的,此时答案一定是环长的因数(否则不能满足题意) 存在入度大于1的DAG图的 一棵树/一条链 很容易发现,最后一种情况想怎么取就怎 ...

  9. Java 大数、高精度模板

    介绍: java中用于操作大数的类主要有两个,一个是BigInteger,代表大整数类用于对大整数进行操作,另一个是BigDecimal,代表高精度类,用于对比较大或精度比较高的浮点型数据进行操作.因 ...

  10. WPF制作带明细的环形图表

    效果 明细用Popup实现的,录gif时,Popup显示不出来,不知道为什么,所以静态图凑合看吧 大体思路 图表使用Arc+Popup实现 图表分为两部分,一是环形部分,一是标注的明细部分. 环形部分 ...