Old Peykan--codeForces 241A--动态规划

Description

There are n cities in the country where the Old Peykan lives. These cities are located on a straight line, we'll denote them from left to right as c1, c2, ..., cn. The Old Peykan wants to travel from city c1 to cn using roads. There are (n - 1) one way roads, the i-th road goes from city ci to city ci + 1 and is di kilometers long.

The Old Peykan travels 1 kilometer in 1 hour and consumes 1 liter of fuel during this time.

Each city ci (except for the last city cn) has a supply of si liters of fuel which immediately transfers to the Old Peykan if it passes the city or stays in it. This supply refreshes instantly k hours after it transfers. The Old Peykan can stay in a city for a while and fill its fuel tank many times.

Initially (at time zero) the Old Peykan is at city c1 and s1 liters of fuel is transferred to it's empty tank from c1's supply. The Old Peykan's fuel tank capacity is unlimited. Old Peykan can not continue its travel if its tank is emptied strictly between two cities.

Find the minimum time the Old Peykan needs to reach city cn.

Input

The first line of the input contains two space-separated integers m and k (1 ≤ m, k ≤ 1000). The value m specifies the number of roads between cities which is equal to n - 1.

The next line contains m space-separated integers d1, d2, ..., dm (1 ≤ di ≤ 1000) and the following line contains m space-separated integers s1, s2, ..., sm (1 ≤ si ≤ 1000).

Output

In the only line of the output print a single integer — the minimum time required for The Old Peykan to reach city cn from city c1.

Sample Input

Input
4 6
1 2 5 2
2 3 3 4
Output
10
Input
2 3
5 6
5 5
Output
14
#include <iostream>
#include <cstdio>
using namespace std;
int value[1008];//用来存到达某个城市的汽油(冲了油后)
int dp[1008];//用来存到达某个城市的最短时间
int dis[1008];//用来存路长度
int s[1008];//用来存每个城市能冲的油
//总体来说,此题是一道贪心的动态规划,如果能到达,就到达。。如果不能到达的话,就在能供油最多的城市多逗留一下
int maxs[1008];//用来存前几个城市供油最多的是哪个城市
int main()
{
	int n,k;//n是路的数目,k是几小时后就能恢复供油
	while(scanf("%d%d",&n,&k)==2)
	{
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&dis[i]);
		}//dis[i]即城市i+1到城市i的距离
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&s[i]);
		}
		maxs[1]=s[1];
		int maxxs=s[1];
		for(int i=2;i<=n;i++)
		{
			if(s[i]>maxxs)
			{
				maxs[i]=s[i];
				maxxs=s[i];
			}
			else maxs[i]=maxxs;
		}
		dp[1]=0;
		value[1]=s[1];
		for(int i=2;i<=n;i++)
		{
			if(value[i-1]>=dis[i-1])
			{
				dp[i]=dp[i-1]+dis[i-1];
				value[i]=value[i-1]-dis[i-1]+s[i];
			}
			else
			{
				while(value[i-1]<dis[i-1])
				{
					value[i-1]+=maxs[i-1];
					dp[i-1]+=k;
				}
				dp[i]=dp[i-1]+dis[i-1];
				value[i]=value[i-1]-dis[i-1]+s[i];
			}
		}
		while(value[n]<dis[n])
		{
			value[n]+=maxs[n];
			dp[n]+=k;
		}
		dp[n+1]=dp[n]+dis[n];
		cout<<dp[n+1]<<endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值