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. centos7搭建kibana

    上一节elasticsearch搭建地址 https://www.cnblogs.com/mutong1228/p/10181544.html 学习了上一篇的搭建,理解了命令的含义之后,本节就非常方便 ...

  2. django_redis作为 session backend 使用配置

    Django 默认可以使用任何 cache backend 作为 session backend, 将 django-redis 作为 session 储存后端不用安装任何额外的 backend # ...

  3. Centos6.8配置HTTPS

    HTTPS服务http+openssl 环境要求:CA证书: 192.168.1.103网站服务器: 192.168.1.104客户端: 192.168.1.107 (linux图形化界面) 内核:( ...

  4. 工具 Sublime日志染色

    工欲善其事,必先利其器. Preferences -> Browse Packages...

  5. [01] Collection和Map

    0.写在前面的话 集合是Java的API中非常重要的概念,用来存储多个数据,并实现了不同的数据结构. Java集合框架中常见的有三大接口: Collection Map Iterator   1.Co ...

  6. b/s程序真的很方便部署吗

    公共应用当然是web系统,这个不说,我说的是企业应用. 最近一些年在企业开发中都提倡web应用,仿佛winform可以结束了,但真的这样吗?最近几天的真实经历如下: 我们部门新开发了一套系统要上线,由 ...

  7. jQuery checkbox全选 和全部取消

    1.chkAll选中,全部chk选中  ,chkAll取消选中,全部chk取消选中 //chkAll选中,全部chk选中 ,chkAll取消选中,全部chk取消选中 $("#chkAll&q ...

  8. hibernate 解决 java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration

    参考:https://stackoverflow.com/questions/9851528/java-lang-noclassdeffounderror-org-hibernate-cfg-conf ...

  9. FreeRTOS 任务与调度器(2)

    在上一篇我们介绍了FreeRTOS任务的一些基本操作和功能,今天我们会介绍一个很好很强大的功能——任务通知 任务通知可以在不同任务之间传递信息,它可以取代二值信号量.计数信号量.事件标志组.深度为1的 ...

  10. 【数据库】Mysql中主键的几种表设计组合的实际应用效果

    写在前面 前前后后忙忙碌碌,度过了新工作的三个月.博客许久未新,似乎对忙碌没有一点点防备.总结下来三个月不断的磨砺自己,努力从独乐乐转变到众乐乐,体会到不一样的是,连办公室的新玩意都能引起莫名的兴趣了 ...