#include #include #include #include using namespace std; using ll=long long; #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() template bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;} template bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;} struct UnionFind { private: vector par, siz; public: UnionFind(int n) : par(n, -1), siz(n, 1) {} int find(int x) { if (par[x] == -1) return x; else { par[x] = find(par[x]); return par[x]; } } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return siz[find(x)]; } bool merge(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } }; const ll INF=1LL<<60; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N,K,C; cin>>N>>K>>C; vector>vt; for(int i=0;i>u>>v>>w>>p; u--; v--; vt.push_back(make_tuple(w,u,v,p)); } UnionFind uf(N); ll cost=0; int ans=0; vector>vt2; vector>>G(N); sort(all(vt)); for(auto[w,u,v,p]:vt){ if(uf.same(u,v)){ vt2.push_back(make_tuple(w,u,v,p)); }else{ uf.merge(u,v); cost+=w; chmax(ans,p); G[u].push_back(make_pair(v,w)); G[v].push_back(make_pair(u,w)); } } if(cost>C){ cout<<-1<<"\n"; return 0; } vectorD(N); vector P(20,vector(N,-1)); vector M(20,vector(N,-INF)); auto dfs=[&](auto dfs,int x,int p,ll w)-> void { P[0][x]=p; M[0][x]=w; for(int i=1;i<20;i++){ if(P[i-1][x]==-1)break; P[i][x]=P[i-1][P[i-1][x]]; } for(int i=1;i<20;i++){ if(P[i-1][x]==-1)M[i][x]=M[i-1][x]; else M[i][x]=max(M[i-1][x],M[i-1][P[i-1][x]]); } for(auto[i,e]:G[x]){ if(i==p)continue; D[i]=D[x]+1; dfs(dfs,i,x,e); } }; dfs(dfs,0,-1,-INF); for(auto[w,u,v,p]:vt2){ if(D[u]>D[v])swap(u,v); ll mx=-INF; for(int i=0;i<20;i++){ if((D[v]-D[u])>>i&1){ chmax(mx,M[i][v]); v=P[i][v]; } } if(u!=v){ for(int i=19;i>=0;i--){ if(P[i][u]!=P[i][v]){ chmax(mx,max(M[i][u],M[i][v])); u=P[i][u]; v=P[i][v]; } } } chmax(mx,max(M[0][u],M[0][v])); if(cost-mx+w<=C)chmax(ans,p); } cout<