#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long Int; typedef vector vint; typedef pair pint; #define mp make_pair template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; } template void chmin(T &t, const T &f) { if (t > f) t = f; } template void chmax(T &t, const T &f) { if (t < f) t = f; } int in() { int x; scanf("%d", &x); return x; } const int LIM_N = 1000; const int LIM_SUM = 1000 * 1000; const Int INF = 1001001001001001001LL; int N; Int M; int A[LIM_N + 10]; Int K[LIM_N + 10]; int ASum[LIM_N + 10]; Int dp[LIM_SUM * 2 + 10]; int main() { for (; ~scanf("%d%lld", &N, &M); ) { for (int i = 0; i < N; ++i) { scanf("%d", &A[i]); } for (int i = 0; i < N; ++i) { scanf("%lld", &K[i]); } partial_sum(A, A + N, ASum + 1); const int sum = ASum[N]; Int target = -M; for (int i = 0; i < N; ++i) { target += A[i] * K[i]; } if (target < 0) { puts("-1"); continue; } memset(dp, 0x3f, sum << 3); dp[0] = 0; for (Int t = target, c = 1; t; t >>= 1, c <<= 1) { memset(dp + sum, 0x3f, sum << 3); for (int i = 0; i < N; ++i) { for (int x = sum + ASum[i + 1]; x >= A[i]; --x) { chmin(dp[x], dp[x - A[i]] + c); } } for (int x = 0; x < sum; ++x) { dp[x] = dp[x << 1 | (t & 1)]; } } if (dp[0] >= INF) { puts("-1"); } else { printf("%lld\n", dp[0]); } } return 0; }