CodeForces

来源:互联网 发布:淘宝店更改主营类目 编辑:程序博客网 时间:2024/06/05 02:25

Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more.

Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?

Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.

Help Inna maximize the total joy the hares radiate. :)

Input

The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 105.

Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number iradiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.

Output

In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.

Example
Input
41 2 3 44 3 2 10 1 1 0
Output
13
Input
78 5 7 6 1 8 92 7 9 5 4 3 12 3 3 4 1 1 3
Output
44
Input
31 1 11 2 11 1 1
Output
4


题意:

给出n个数,每个数取走的贡献与相邻的数有关,如果取这个数的时候,左右的数都还没被取,那么权值为a,如果左右两个数有一个被取走了,那么权值为b,如果左右两个数都被取走了,那么权值为c,求取取走全部数的最大值。

下面描述“取走”就是这只兔子喂过了,“没取走”就是这只兔子没有被喂过

//这道题主要是状态转移;
//dp[i][j][k] i表示第几只兔子,j表示当喂这个兔子时,它旁边的兔子的状态,
// k表示是左还是右; 
// dp[i][0][0]  表示当喂第i只兔子时,两边兔子都是饥饿的 ; 
//dp[i][1][0]  表示当喂第i只兔子时,左边兔子已经喂过了; 
//dp[i][1][1] 表示当喂第i只兔子时,右边兔子已经喂过了 ; 
//dp[i][2][0] 表示当第i只兔子时,两边的兔子都已经喂过了; 

代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define INF 1<<29int dp[3005][3][3];int a[3][3005],n;int main(){int i,j,k,t;int tt=INF;tt=-tt;while(~scanf("%d",&n)){for(i=0;i<3;i++)for(j=1;j<=n;j++)scanf("%d",&a[i][j]);memset(dp,tt,sizeof(dp));dp[1][0][0]=a[0][1]; //第一只兔子只有这两种状态dp[1][1][1]=a[1][1];dp[1][1][0]=dp[1][2][0]=tt;for(i=2;i<=n;i++){dp[i][0][0]=max(dp[i-1][1][1]+a[0][i],dp[i-1][2][0]+a[0][i]);dp[i][1][0]=max(dp[i-1][1][0]+a[1][i],dp[i-1][0][0]+a[1][i]);dp[i][1][1]=max(dp[i-1][1][1]+a[1][i],dp[i-1][2][0]+a[1][i]);dp[i][2][0]=max(dp[i-1][1][0]+a[2][i],dp[i-1][0][0]+a[2][i]);}int ans;ans=max(dp[n][0][0],dp[n][1][0]); //最后一只兔子只有这两种状态printf("%d\n",ans);}return 0;} //dp[i][0][0]=max(dp[i-1][1][1]+a[0][i],dp[i-1][2][0]+a[0][i]);//dp[i][1][0]=max(dp[i-1][1][0]+a[1][i],dp[i-1][0][0]+a[1][i]);//dp[i][1][1]=max(dp[i-1][1][1]+a[1][i],dp[i-1][2][0]+a[1][i]);//dp[i][2][0]=max(dp[i-1][1][0]+a[2][i],dp[i-1][0][0]+a[2][i]);//解释一下这些状态转移,以第一个为例;//dp[i][0][0]表示是当喂第i只兔子时,它两边的兔子都还没喂,看这个状态是由前一只兔子的那些状态转移过来的啊; //当喂前一只兔子时,这一只兔子一定喂过了,所以由前一只兔子的两种状态的一种转变过来,//那两种状态啊:一:当喂前一只兔子时,这一只兔子一定喂过了dp[i-1][1][1],二:当喂前一只兔子时,//前一只兔子两边的兔子都喂过了 dp[i-1][2][0];// 第二个: //dp[i][1][0],表示当喂这只兔子时,它左边已经喂,那么当喂它左边(前一个)兔子时,这一只一定没有喂// 所以可能由前一只兔子的两种状态转移过来,一:dp[i-1][1][0] 当喂前一只兔子时,前一只兔子左边的兔子已经喂过,// 二:dp[i-1][2][0] 表示当喂前一只兔子时,前一只兔子两边的兔子都没有喂;//第三个://dp[i][1][1] 表示当喂这只兔子时,它右边的已经喂过了,而它前一个还没有喂,那么当喂前一只兔子时,这只一定喂过了 // 所以由前一个兔子的两种状态,一:当喂前一只兔子时, 这只一定喂过了,dp[i-1][1][1] // 二:当喂前一只兔子时,可能两边的兔子都喂过了dp[i-1][2][0];// 第四个自己想着推吧,好吧!