Codeforces 358D 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.

Examples
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

题目大意:

一共有N个物品排成一排,对应拿当前物品,如果相邻两个物品都没有被拿过,那么得到的价值为ai,如果相邻的两个物品有一个被拿过(左右无所谓),那么得到的价值为bi,如果相邻的两个物品都被拿走了,那么对应价值为ci、问将所有物品都拿走能够获得的最高价值为多少。


思路:


1、考虑当前拿第i个物品的时候,相关影响这个物品能够得到的价值的多少只和其相邻的两个物品是否被拿了相关,那么我们考虑设定dp【i】【2】【2】:

①dp【i】【0】【0】表示拿第i个物品的时候,左边和右边的物品都没有被拿

②dp【i】【0】【1】表示拿第i个物品的时候,左边的物品没有被拿,右边的物品被拿了

③dp【i】【1】【0】表示拿第i个物品的时候,左边的物品被拿了,右边的物品没有被拿

④dp【i】【1】【1】表示拿第i个物品的时候,左右两边的物品都被拿了


2、那么考虑其状态转移方程:
①dp【i】【0】【0】=max(dp【i-1】【1】【1】+a【i】,dp【i-1】【0】【1】+a【i】)

表示当前拿第i个物品的时候,左右两边的物品都没有拿,那么拿第i-1个物品的时候,第i个物品一定是拿完了的,相当于第i个物品在第i-1个物品之前拿了。

②dp【i】【0】【1】=max(dp【i-1】【1】【1】+b【i】,dp【i-1】【0】【1】+b【i】)

表示当前拿第i个物品的时候,右边的物品拿了,那么拿第i-1个物品的时候,第i个物品一定是拿完了的,相当于第i个物品在第i-1个物品之前拿了。

③dp【i】【1】【0】=max(dp【i-1】【1】【0】+b【i】,dp【i-1】【0】【0】+b【i】)

表示当前拿第i个物品的时候,左边的物品已经拿了,那么拿第i-1个物品的时候,其第i个物品一定是没有被拿的,相当于第i-1个物品在第i个物品之前拿了。

④dp【i】【1】【1】max(dp【i-1】【1】【0】+b【i】,dp【i-1】【0】【0】+b【i】)

表示当前拿第i个物品的时候,左边的和右边的物品都已经拿了,那么拿第i-1个物品的时候,其第i个物品一定是没有被拿的,相当于第i-1个物品在第i个物品之前拿了。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int a[3005];
int b[3005];
int c[3005];
int dp[3005][2][2];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)scanf("%d",&b[i]);
        for(int i=1;i<=n;i++)scanf("%d",&c[i]);
        memset(dp,-0x3f3f3f3f,sizeof(dp));
        dp[1][0][0]=a[1];
        dp[1][0][1]=b[1];
        for(int i=2;i<=n;i++)
        {
            dp[i][0][0]=max(dp[i-1][0][1]+a[i],dp[i-1][1][1]+a[i]);

            dp[i][0][1]=max(dp[i-1][0][1]+b[i],dp[i-1][1][1]+b[i]);

            dp[i][1][0]=max(dp[i-1][0][0]+b[i],dp[i-1][1][0]+b[i]);

            dp[i][1][1]=max(dp[i-1][0][0]+c[i],dp[i-1][1][0]+c[i]);
        }
        printf("%d\n",max(dp[n][0][0],dp[n][1][0]));
    }
}





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值