Codeforces 358 D. Dima and Hares,codeforcesdima

來源:互聯網
上載者:User

Codeforces 358 D. Dima and Hares,codeforcesdima


dp[i][0]表示i號兔子先於i-1號兔子餵食,dp[i][1]反過來.

倒著DP

D. Dima and Harestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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 iradiates 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
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

/** * Created by ckboss on 14-10-6. */import java.util.*;public class DimaandHares {    public static void main(String[] args){        Scanner in = new Scanner(System.in);        int n=in.nextInt();        int[] A=new int[n+10],B=new int[n+10],C=new int[n+10];        int[][] dp = new int[n+10][2];        for(int i=1;i<=n;i++) A[i]=in.nextInt();        for(int i=1;i<=n;i++) B[i]=in.nextInt();        for(int i=1;i<=n;i++) C[i]=in.nextInt();        dp[n][0]=A[n]; dp[n][1]=B[n];        for(int i=n-1;i>0;i--)        {            dp[i][0]=Math.max(dp[i][0],dp[i+1][0]+B[i]);            dp[i][0]=Math.max(dp[i][0],dp[i+1][1]+A[i]);            dp[i][1]=Math.max(dp[i][1],dp[i+1][1]+B[i]);            dp[i][1]=Math.max(dp[i][1],dp[i+1][0]+C[i]);        }        System.out.println(dp[1][0]);    }}






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.