// #pragma GCC target("avx2") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") #include using namespace std; // #include // #include // #include // #include // #include // #include // #include // #include // using namespace atcoder; // #include // using mint = atcoder::modint998244353; // using mint = atcoder::modint1000000007; // using mint = atcoder::modint; // #include // using Bint = boost::multiprecision::cpp_int; // #include // using Rat = boost::rational; // #include // using Real = boost::multiprecision::number>; #ifdef LOCAL #include #define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (static_cast(0)) #endif using ll = long long; using ld = long double; const ll INF = 4e18; const int INF_INT = 2e9; using vi = vector; using vd = vector; using vl = vector; using vvi = vector>; using vvl = vector>; using vs = vector; using vp = vector>; using vpc = vector>; using vb = vector; using vvb = vector>; using pl = pair; template using PQ = priority_queue; template using GPQ = priority_queue, greater>; struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } fast; // interactive #define endl "\n" #define INTYPE(type, ...) type __VA_ARGS__; #define INT(...) INTYPE(int, __VA_ARGS__) IN(__VA_ARGS__) #define LL(...) INTYPE(ll, __VA_ARGS__) IN(__VA_ARGS__) #define STR(...) INTYPE(string, __VA_ARGS__) IN(__VA_ARGS__) #define CHR(...) INTYPE(char, __VA_ARGS__) IN(__VA_ARGS__) #define DBL(...) INTYPE(double, __VA_ARGS__) IN(__VA_ARGS__) #define LD(...) INTYPE(ld, __VA_ARGS__) IN(__VA_ARGS__) #define overload6(_1, _2, _3, _4, _5, _6, name, ...) name #define INVEC(type, ...) vector __VA_ARGS__; #define VEC1(type, name, size) INVEC(type, name(size)) rep(I, size) IN(name[I]) #define VEC2(type, name1, name2, size) INVEC(type, name1(size), name2(size)) rep(I, size) IN(name1[I], name2[I]) #define VEC3(type, name1, name2, name3, size) INVEC(type, name1(size), name2(size), name3(size)) rep(I, size) IN(name1[I], name2[I], name3[I]) #define VEC4(type, name1, name2, name3, name4, size) INVEC(type, name1(size), name2(size), name3(size), name4(size)) rep(I, size) IN(name1[I], name2[I], name3[I], name4[I]); #define VEC(...) overload6(__VA_ARGS__, VEC4, VEC3, VEC2, VEC1)(__VA_ARGS__) #define VV(type, name, h, w) INVEC(vector, name(h, vector(w))) rep(I, h) rep(J, w) IN(name[I][J]); #define vec(type, name, ...) vector name(__VA_ARGS__) #define vv(type, name, h, ...) vector name(h, vector(__VA_ARGS__)) #define vvv(type, name, h, w, ...) vector name(h, vector>(w, vector(__VA_ARGS__))) #define overload4(_1, _2, _3, _4, name, ...) name #define rep1(i, n) rep2(i, 0, n) #define rep2(i, a, b) for (ll i = ll(a); i < ll(b); ++i) #define rep3(i, a, b, c) for (ll i = ll(a); i < ll(b); i += ll(c)) #define rep(...) overload4(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__) #define rrep1(i, n) rrep2(i, 0, n) #define rrep2(i, a, b) for (ll i = ll(b) - 1; i >= ll(a); --i) #define rrep3(i, a, b, c) for (ll i = ll(b) - 1; i >= ll(a); i -= ll(c)) #define rrep(...) overload4(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define llacumulate(v) accumulate(all(v), 0LL) #define sz(x) (ll)(x).size() #define ctoi(c) (c - '0') #define itoc(i) (i + '0') #define emp(x) x.empty() #define nmod(x, m) ((x % m + m) % m) #define fi first #define se second #define pf push_front #define ef emplace_front #define pof pop_front #define pb push_back #define eb emplace_back #define pob pop_back #define pc (ll) __builtin_popcountll #define yes cout << "Yes" << endl; #define no cout << "No" << endl; bool yn(bool f) { cout << (f ? "Yes" : "No") << endl; return f; } bool YN(bool f) { cout << (f ? "YES" : "NO") << endl; return f; } bool kaibun(string s) { return s == string(rall(s)); } bool inGrid(ll i, ll j, ll h, ll w) { return i < h && j < w && i >= 0 && j >= 0; } const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; template void IN(T &...a) { (cin >> ... >> a); } string toUpper(string s) { rep(i, s.size()) s[i] = toupper(s[i]); return s; } string toLower(string s) { rep(i, s.size()) s[i] = tolower(s[i]); return s; } template auto min(const T &a) { return *min_element(all(a)); } template auto max(const T &a) { return *max_element(all(a)); } template bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } vector> runLength(const string &str) { ll n = str.size(); vector> res; for (ll l = 0; l < n;) { ll r = l + 1; while (r < n && str[l] == str[r]) ++r; res.emplace_back(str[l], r - l); l = r; } return res; } template int getNearest(const T &a, const vector &v) { int x = lower_bound(all(v), a) - v.begin(), n = v.size(); if (x == 0) return 0; if (x == n) return n - 1; if (abs(v[x] - a) < abs(v[x - 1] - a)) return x; else return x - 1; } template vector> primeFactorize(T n) { vector> res; for (ll i = 2; i * i <= n; ++i) { if (n % i != 0) continue; ll cnt = 0; while (n % i == 0) { n /= i; ++cnt; } res.emplace_back(i, cnt); } if (n != 1) res.emplace_back(n, 1); return res; } template bool isPrime(const T &n) { if (n < 2) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (ll i = 3; i * i <= n; ++i) if (n % i == 0) return false; return true; } vector eratosthenes(ll n) { vector v(n + 1, true); v[0] = v[1] = false; for (ll p = 2; p <= n; ++p) { if (!v[p]) continue; for (ll q = p * 2; q <= n; q += p) v[q] = false; } return v; } template vector divisor(T n) { vector res; for (ll i = 1; i * i <= n; ++i) { if (n % i == 0) { res.emplace_back(i); if (i * i != n) res.emplace_back(n / i); } } sort(res.begin(), res.end()); return res; } ll llpow(ll x, ll n) { if (n == 0) return 1; ll res = llpow(x, n / 2); res *= res; if (n % 2 == 1) res *= x; return res; } template T getCycle(const vector &v, const S &k) { T n = v.size(), s = v.back(), i; for (i = n - 2; i >= 0; --i) { if (v[i] == s) break; } if (k < i) return v[k]; else return v[i + (k - i) % (n - i - 1)]; } void print() { cout << endl; } template inline void print(const T &x) { cout << x << endl; } template void print(const T &a, const Ts &...b) { cout << a; (cout << ... << (cout << ' ', b)); cout << endl; } template inline void print(const vector &v) { rep(i, v.size()) { if (i != (ll)v.size() - 1) cout << v[i] << " "; else cout << v[i] << endl; } } template inline void print(const vector> &v) { for (auto &&p : v) print(p); } template inline void print(const pair &p) { cout << p.first << " " << p.second << endl; } template inline void print(const vector> &v) { for (auto &&p : v) print(p); } int main() { LL(n, p, k); VEC(ll, t, b, n); vv(ll, dp, n + 1, k + 10, -INF); dp[0][0] = p; const ll m = 1e18 + 1; rep(i, n) { rep(j, k + 1) { if (dp[i][j] == -INF) continue; // 選ぶ if (t[i] == 1) chmax(dp[i + 1][j + 1], min(m, dp[i][j] + b[i])); else chmax(dp[i + 1][j + 1], min(m, dp[i][j] * 2)); // 選ばない if (t[i] == 1) chmax(dp[i + 1][j], dp[i][j]); else chmax(dp[i + 1][j], dp[i][j]); } } debug(dp); ll ans = dp[n][k]; if (ans == m) ans = -1; print(ans); }