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
6
input
Copy
4 1000 10 35
1 2 4 5
1000 500 250 200
output
5
input
Copy
2 10 7 11
2 10
6 1
output
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]只鸟,每个鸟窝唤醒一只鸟的代价为cost[i].一个人有初始法力值w,他从第一个鸟窝出发走向第n个鸟窝,当他从i到i+1鸟窝的时候会恢复x点法力值.唤醒一只鸟,他的法力容量增大b. 问他最多能唤醒多少只鸟?

思路:

dp[i][j]:到第i个鸟窝,唤醒j只鸟所能保留的最大法力值

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

复杂度不足1e9的样子

至于dp数组memset -1的原因是最后要判>=0,如果初始为0的话,比如dp[n][sum[n]]实际上是无法转移到的,0始终是最大值,但发现memset的时候dp是0的话,就输出错误了

#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define bug cout << "bug" << endl
#pragma comment(linker, "/STACK:102400000,102400000")


using namespace std;
typedef long long ll;

const int MAX_N=1e3+3;
ll c[MAX_N];
ll cost[MAX_N];
ll sum[MAX_N];
ll dp[MAX_N][MAX_N*10];
int main(void){
    ll n,w,b,x;
    cin >> n>>w>>b>>x;
    for(int i=1;i<=n;i++)   scanf("%lld",&c[i]),sum[i]+=sum[i-1]+c[i];
    for(int j=1;j<=n;j++)   scanf("%lld",&cost[j]);
    memset(dp,-1,sizeof(dp));
    dp[0][0]=w;
    for(int i=1;i<=n;++i){
        for(int j=0;j<=sum[i];++j){
            for(int k=0;k<=c[i]&&k<=j;++k){
                if(dp[i-1][j-k]>=0) dp[i][j]=max(dp[i][j],min(dp[i-1][j-k]+x,w+(j-k)*b)-cost[i]*k);
            }
        }
    }
    for(int i=sum[n];i>=0;i--){
        if(dp[n][i]>=0){
            cout << i << endl;
            return 0;
        }
    }
}