CodeForces

来源:互联网 发布:阿迪达斯淘宝旗舰店 编辑:程序博客网 时间:2024/06/10 20:21

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 i radiates 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只兔子,兔子的开心值受到相邻伙伴的影响,求n只兔子开心值的最大值。

             第二行:旁边兔子未吃饱;第三行:一只相邻的兔子吃饱;第四行:两只相邻的兔子都吃饱。

//状态转移;
//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;int n;int dp[3100][5][3];int a[3][3100];int main(){    while(~scanf("%d",&n))    {        for(int i=0; i<3; i++)            for(int j=1; j<=n; j++)                scanf("%d",&a[i][j]);        memset(dp,0,sizeof(dp));        dp[1][0][0]=a[0][1]; //第一只兔子只有这两种状态        dp[1][1][1]=a[1][1];         for(int 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];    // 第四个:// dp[i][2][0]表示当喂第i只兔子时,它两边的兔子都喂过了,当喂它前一个时,这只兔子没有喂过;dp[i-1][1][0]表示//前一只兔子的左边已喂过,dp[i-1][0][0]表示左右两边都没喂过。