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.
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).
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.
4 6 1 2 5 2 2 3 3 4
10
2 3 5 6 5 5
14
In the second sample above, the Old Peykan stays in c1 for 3 hours.
/***
Codeforce 241A #241中最简单的一道。
感觉是很经典的贪心问题,所以就保存下来了。
题目思路:
总时间一定是总路程s+刷新次数*刷新时间k
什么时候需要刷新?
当然是到达某一条路时,剩余的汽油无法到达下一条路;
在哪里进行刷新等待?
为了保证时间最小,我们必须保证刷新后,剩余油量是当前这段路中,汽油总量所能达到的最大值,
因此,必须是在无法到达下一条路的位置之前的某一个点,这个点,拥有从开始到断点为止的最大储油数。
解决上面两个问题后,具体做法就显而易见了:
累加每一条路径同时累加油量,并且记录最大值,加一次判断当前油量是否小于路径,如果是,就需要
将油量加上最大值,在进行油量和路径的判断,直到油量大于等于路径。记录增加最大值的次数,
***/
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define eps 1e-8
#define inf 0x7fffffff
#define Mem(a,x) memset(a,x,sizeof(a))
#define maxn 100000
struct Node
{
int c;
int d;
}node[1010];
int m,k;
ll t=0;
int deal()
{
int i ;
int s=0;
int dis=0;
int maxc=0;
for(i=1;i<=m;i++)
{
if(node[i].c>maxc)
maxc=node[i].c;//记录最大值
dis+=node[i].d;//累加路径
s+=node[i].c;//累加油量
while(s<dis)
{
s+=maxc;
t++;//累计刷新次数
}
}
s=dis+t*k;//计算总时间
return s;
}
int main()
{
while(~scanf("%d%d",&m,&k))
{
for(int i=1;i<=m;i++)
scanf("%d",&node[i].d);
for(int i=1;i<=m;i++)
scanf("%d",&node[i].c);
printf("%d\n",deal());
}
return 0;
}