Codeforce 214A Old Peykan

A. Old Peykan
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
input
4 6
1 2 5 2
2 3 3 4
output
10
input
2 3
5 6
5 5
output
14
Note

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;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值