Codeforces Round #461 (Div. 2)E. Birds(dp)

题目:点击打开链接

E. Birds
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Apart from plush toys, Imp is a huge fan of little yellow birds!

To summon birds, Imp needs strong magic. There are n trees in a row on an alley in a park, there is a nest on each of the trees. In the i-th nest there are ci birds; to summon one bird from this nest Imp needs to stay under this tree and it costs him costi points of mana. However, for each bird summoned, Imp increases his mana capacity by B points. Imp summons birds one by one, he can summon any number from 0 to ci birds from the i-th nest.

Initially Imp stands under the first tree and has W points of mana, and his mana capacity equals W as well. He can only go forward, and each time he moves from a tree to the next one, he restores X points of mana (but it can't exceed his current mana capacity). Moving only forward, what is the maximum number of birds Imp can summon?

Input

The first line contains four integers nWBX (1 ≤ n ≤ 103, 0 ≤ W, B, X ≤ 109) — the number of trees, the initial points of mana, the number of points the mana capacity increases after a bird is summoned, and the number of points restored when Imp moves from a tree to the next one.

The second line contains n integers c1, c2, ..., cn (0 ≤ ci ≤ 104) — where ci is the number of birds living in the i-th nest. It is guaranteed that .

The third line contains n integers cost1, cost2, ..., costn (0 ≤ costi ≤ 109), where costi is the mana cost to summon a bird from the i-th nest.

Output

Print a single integer — the maximum number of birds Imp can summon.

Examples
input
Copy
2 12 0 4
3 4
4 2
output
Copy
6
input
Copy
4 1000 10 35
1 2 4 5
1000 500 250 200
output
Copy
5
input
Copy
2 10 7 11
2 10
6 1
output
Copy
11
Note

In the first sample base amount of Imp's mana is equal to 12 (with maximum capacity also equal to 12). After he summons two birds from the first nest, he loses 8 mana points, although his maximum capacity will not increase (since B = 0). After this step his mana will be 4 of 12; during the move you will replenish 4 mana points, and hence own 8 mana out of 12 possible. Now it's optimal to take 4 birds from the second nest and spend 8 mana. The final answer will be — 6.

In the second sample the base amount of mana is equal to 1000. The right choice will be to simply pick all birds from the last nest. Note that Imp's mana doesn't restore while moving because it's initially full.

【题意】

n棵树,每棵树上有c[i]只鸟,捉第i棵树上一只鸟的法力消耗是cost[i]。

初始法力值w,也是最大法力值。每捉一只鸟,最大法力限制值增加B,每走一棵树,法力值回复X

人初始在第1棵树,只能朝n的方向走。问最多能捉几只鸟

【分析】

题意有些复杂,乍一看像背包。

dp[i][j]表示前i棵树,捉到j只鸟时,剩余的法力值,初值为负无穷。只要dp[i][j]为正数,则该状态是可以实现的。

状态转移:

if ( dp[i-1][j-k] >=0 )

    dp[i][j] = max( dp[i][j], min(w+(j-k)*b, dp[i-1][j-k]+x) - k*cost[i] ); 

【代码】

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=0x3f3f3f3f;
ll n,w,b,x;
ll dp[1010][10101];
ll c[1010],cost[1010];
int main()
{
    cin>>n>>w>>b>>x;
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&c[i]);sum+=c[i];
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&cost[i]);
    }
    memset(dp,-INF,sizeof(dp));
    dp[0][0]=w;
    for(int i=1;i<=n;i++) //枚举树
    {
        for(int j=sum;j>=0;j--) //枚举前i棵树捉了j只鸟
        {
            for(int k=c[i];k>=0;k--) //枚举第i棵树捉k只鸟
            {
                if(dp[i-1][j-k]>=0)
                    dp[i][j]=max(dp[i][j],min(w+(j-k)*b,dp[i-1][j-k]+x)-k*cost[i]);
            }
        }
    }
    for(int j=sum;j>=0;j--)if(dp[n][j]>=0){
        printf("%d\n",j);
        break;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪的期许

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值