#include #include #include #include #include #include #define ll long long using namespace std; typedef struct tga{ ll s,t,y,m; } tga; int main(){ ll N,C,V,numCount=0,ansTime=-1; cin>>N>>C>>V; vector st,yst,mst,ist; vector v(V); for(int i=0;i>v[i].s; } for(int i=0;i>v[i].t; } for(int i=0;i>v[i].y; } for(int i=0;i>v[i].m; } sort(v.begin(),v.end(), [](const tga& a,const tga& b) {return a.t< b.t;}); st.push_back(N); for(int i=0;i=accumulate(yst.begin(),yst.end(),0)){ if(ansTime==-1){ ansTime=accumulate(mst.begin(),mst.end(),0); } else{ if(ansTime>accumulate(mst.begin(),mst.end(),0)) ansTime=accumulate(mst.begin(),mst.end(),0); } } numCount++; st.pop_back(); i=ist[ist.size()-1]; ist.pop_back(); yst.pop_back(); mst.pop_back(); } else if(i==V-1){ if(st[st.size()-1]==N) break; st.pop_back(); i=ist[ist.size()-1]; ist.pop_back(); yst.pop_back(); mst.pop_back(); } } printf("%lld",ansTime); }