#include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; const ll INF=1LL<<60; typedef pair P; typedef pair PP; const ll MOD=998244353; int main(){ int N,M,K,T; cin>>N>>M>>K>>T; vector> back(N,vector

(M,make_pair(0,0))); for(int i=0;i>a>>b>>c>>d; a--;b--; back[a][b]=make_pair(c,d); } ll offset=N+M+2; vector dp(N,vector>(M,vector(2*(N+M)+10,INF))); typedef tuple TP; priority_queue,greater> pq; pq.emplace(0,0,0,0); while(!pq.empty()){ auto [val,nowi,nowj,nowt] = pq.top();//valは負の値.ちいさいほうから pq.pop(); //ll cost=-val; ll cost=val; if(dp[nowi][nowj][nowt+offset]<=cost) continue; //dp[nowi][nowj][nowt+offset]>cost dp[nowi][nowj][nowt+offset]=cost; if(nowi+1dp[nowi][nowj][nowt+offset] ){ pq.emplace(dp[nowi][nowj][nowt+offset],ni,nj,nowt+1); } }else{ auto [ci,di]=back[nowi][nowj]; ll totime = nowt-ci+1; if(totime<-(N+M-2)){ totime=-(N+M-2); } if(dp[ni][nj][totime+offset]>(dp[nowi][nowj][nowt+offset]+di) ){ pq.emplace((dp[nowi][nowj][nowt+offset]+di),ni,nj,totime); } } } if(nowj+1dp[nowi][nowj][nowt+offset] ){ pq.emplace(dp[nowi][nowj][nowt+offset],ni,nj,nowt+1); } }else{ auto [ci,di]=back[nowi][nowj]; ll totime = nowt-ci+1; if(totime<-(N+M-2)){ totime=-(N+M-2); } if(dp[ni][nj][totime+offset]>(dp[nowi][nowj][nowt+offset]+di) ){ pq.emplace((dp[nowi][nowj][nowt+offset]+di),ni,nj,totime); } } } } ll ans=INF; /* for(int t=0;t<2*(N+M)+10 && t<=T-offset;t++){ ans=min(ans,dp[N-1][M-1][t]); } */ for(int t=0;t<2*(N+M)+10 && t<=T+offset;t++){ ans=min(ans,dp[N-1][M-1][t]); } cout<<(ans==INF?-1:ans)<