Tickets

Red Cliff (lower) finally released in Ningbo, and that the box office is really booming ah, then that would be difficult to get a ticket. Xiao Ming ran excitedly "XX Cinema" (To avoid advertising suspects, the name of the theater play mosaics, is now available for leasing advertising, price concessions, welcome to inquire theaters) door. There are n already found fans queue up for tickets, a person bought one, and the provisions of the ticket office, a person can only buy a maximum of two tickets. I assume that the first digit fan buy a ticket takes time Ti (1≤i≤n), the team of two adjacent fans (j-j + 1 personal and individual) can also be made where a person to buy two tickets, while another can not line up, and the two fans to buy two tickets in time becomes Rj, if Rj <Tj + Tj + 1, doing so can shorten the waiting time behind the fans, to speed up the whole process of ticket .

Too many people. . Xiao Ming came in behind this individual n, that heartburn is really, ah, Xiao Ming in front of people and do not like to buy a ticket together, and that only after the n individuals to buy tickets to turn Xiaoming tickets. Nickname may want to buy tickets early, now given n, Tj and Rj, Xiao Ming to help you calculate how long the fastest he can turn to buy a ticket?

Input

有多组数据,每组包含3行:第一行一个整数n(1<=n<=5000);第二行n个整数,表示Ti(1≤i≤n,0<=Ti<=65535)
相邻两个数之间有一个空格隔开;第三行n-1个整数,表示Ri(1≤i≤n-1,0<=Ri<=65535)相邻两个数之间有一个空格隔开。

Output

每组输入对应一组输出,每一组输出只有一行一个整数k,表示n个人买完票最快需要多少时间。

Sample Input

3
1 2 3
1 1
4
1 2 3 4
2 3 10

Sample Output

2
8
#include<bits/stdc++.h>
using namespace std;
int i,i0,n,m,T,a[5005],r[5005];
long long dp[5005];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1;i<=n;i++)scanf("%d",&a[i]),dp[i]=(long long)n*INT_MAX;
        for(i=1;i<n;i++)scanf("%d",&r[i]);
        for(i=1;i<=n;i++)
        {
            dp[i]=min(dp[i],dp[i-1]+a[i]);
            if(i>1)dp[i]=min(dp[i],dp[i-2]+r[i-1]);
        }
        printf("%lld\n",dp[n]);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44061561/article/details/94555355