CF 358 D Dima and Hares(dp)

D. Dima and Hares
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Sample test(s)
input
4
1 2 3 4
4 3 2 1
0 1 1 0
output
13
input
7
8 5 7 6 1 8 9
2 7 9 5 4 3 1
2 3 3 4 1 1 3
output
44
input
3
1 1 1
1 2 1
1 1 1
output
4


/*
思路:

dp[i][0][0] 代表吃这个的时候左右都没有吃
dp[i][1][0] 代表吃这个的时候 左边已经吃了右边没吃
dp[i][1][1] 代表吃这个的时候 右边已经吃了左边没吃
dp[i][2][0] 代表吃这个的时候,左右都吃了


*/


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")
#define mod 1000000007
#define eps 1e-8

typedef __int64 ll;

using namespace std;
#define INF 0x3f3f3f3f
#define N 3005
#define mod 1000000007

int dp[N][3][2];

int a[N][3];
int n;

int main()
{
    int i,j;
    while(~scanf("%d",&n))
    {
          for(i=0;i<3;i++)
              for(j=1;j<=n;j++)
                 scanf("%d",&a[j][i]);
        dp[1][0][0]=a[1][0];
        dp[1][1][0]=-INF;
        dp[1][1][1]=a[1][1];
        dp[1][2][0]=-INF;

        for(i=2;i<=n;i++)
        {
            dp[i][0][0]=max(dp[i-1][2][0]+a[i][0],dp[i-1][1][1]+a[i][0]);
            dp[i][1][0]=max(dp[i-1][0][0]+a[i][1],dp[i-1][1][0]+a[i][1]);
            dp[i][1][1]=max(dp[i-1][1][1]+a[i][1],dp[i-1][2][0]+a[i][1]);
            dp[i][2][0]=max(dp[i-1][1][0]+a[i][2],dp[i-1][0][0]+a[i][2]);
        }
       printf("%d\n",max(dp[n][0][0],dp[n][1][0]));
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值