#include #include #include #include #include #include #define ll long long using namespace std; struct tga { ll s; ll t; ll y; ll m; }; int main() { ll countNum = 0; ll anstime=-1; ll V, C, N; cin >> N >> C >> V; vector vec(V); for (ll i = 0; i < V; i++) { cin >> vec[i].s; } for (ll i = 0; i < V; i++) { cin >> vec[i].t; } for (ll i = 0; i < V; i++) { cin >> vec[i].y; } for (ll i = 0; i < V; i++) { cin >> vec[i].m; } sort(vec.begin(),vec.end(), [](const struct tga& a,const struct tga& b) {return a.t< b.t;}); vector st,cst,tst; st.push_back(N); vector havei; for(ll i=0;i=accumulate(cst.begin(),cst.end(),0)){ if(anstime==-1){ anstime=accumulate(tst.begin(),cst.end(),0); } else if(anstime>accumulate(tst.begin(),cst.end(),0)){ anstime=accumulate(tst.begin(),cst.end(),0); } } countNum++; st.pop_back(); i=havei[havei.size()-1]; havei.pop_back(); cst.pop_back(); tst.pop_back(); } //end loop else if(i==V-1){ if(st[st.size()-1]==N) break; st.pop_back(); i=havei[havei.size()-1]; havei.pop_back(); cst.pop_back(); tst.pop_back(); } } anstime=0; printf("%lld\n", anstime); return 0; } /* int tagawaaaaa(ll pos) { if (pos == 1) { countNum++; if (myCost <= C) { if (ansTime == -1) ansTime = myTime; else { if (ansTime > myTime) ansTime = myTime; } } return 0; } for (ll i = 0; vec[i].t<=pos; i++) { if (pos == vec[i].t) { if (pos == N) { myTime = 0; myCost = 0; } myTime += vec[i].m; myCost += vec[i].y; tagawaaaaa(vec[i].s); } } return 0; } */