#include using namespace std; struct HLD2{ //2.辺のみパターン. //辺のコストを頂点にするのが楽そう. vector> Graph; int n = 0,tim = 0; vector dist,in,out,siz,head,pre,par; vector cost; long long e(){return 0;} void rearrange(vector>> &G){ n = G.size()*2-1; Graph.resize(n); cost.resize(n,e()); int edge = G.size(); for(int i=0; i make(vector>> &G){ rearrange(G); dist.resize(n),in.resize(n),siz.resize(n,1); head.resize(n),par.resize(n); dfs1(0,-1,0); dfs2(0,-1); vector ret(n); for(int i=0; i> findpath(int u,int v){ //dfs行きがけ順に並べた頂点のセグ木の区間を返す. //行きがけ順はrep(0-n)give[in[i]]=A[i]. //交換法則が成り立たない時は修正必須. vector> ret; while(head.at(u) != head.at(v)){ if(dist.at(head.at(u)) > dist.at(head.at(v))) swap(u,v); ret.push_back({in.at(head.at(v)),in.at(v)+1}); v = par.at(head.at(v)); } if(in.at(u) > in.at(v)) swap(u,v); ret.push_back({in.at(u),in.at(v)+1}); return ret; } }; using SS = long long; class SegmentTree{ public: int siz = 1; vector dat; SS op(SS a, SS b){return max(a,b);} SS e(){return 0;} void renew (SS &a,SS x){ //a = op(a,x); a = x; //その他. } void make(int N){ while(siz < N) siz *= 2; dat.resize(siz*2,e()); } void make2(int N,vector &A){ make(N); for(int i=0; i0; i--) dat.at(i) = op(dat.at(i*2),dat.at(i*2+1)); } void update(int pos,SS x){ pos = pos+siz; renew(dat.at(pos),x); while(pos != 1){ pos = pos/2; dat.at(pos) = op(dat.at(pos*2),dat.at(pos*2+1)); } } SS findans(int l, int r){ SS retl = e(),retr = e(); l += siz,r += siz; while(l < r){ if(l&1) retl = op(retl,dat.at(l++)); if(r&1) retr = op(dat.at(--r),retr); l >>= 1; r >>= 1; } return op(retl,retr); } SS get(int pos){return dat.at(pos+siz);} SS rangeans(int l, int r){return findans(l,r);} SS allrange(){return findans(0,siz);} //ノーマルセグ木 二分探索は実装してない. }; class UnionFind{ public: vector par,siz; void make(int N){ par.resize(N,-1); siz.resize(N,1); } int root(int x){ if(par.at(x) == -1) return x; else return par.at(x) = root(par.at(x)); } void unite(int u, int v){ u = root(u),v = root(v); if(u == v) return; if(siz.at(u) < siz.at(v)) swap(u,v); par.at(v) = u; siz.at(u) += siz.at(v); } bool issame(int u, int v){ if(root(u) == root(v)) return true; else return false; } }; int main() { long long N,K,C; cin >> N >> K >> C; UnionFind Z; Z.make(N); vector> WPUV(K); for(auto &[w,p,u,v] : WPUV) cin >> u >> v >> w >> p,u--,v--; sort(WPUV.begin(),WPUV.end()); long long nowc = 0; int nowp = -1; vector already; vector>> Graph(N); for(auto &[w,p,u,v] : WPUV){ if(Z.issame(u,v)){already.push_back(false); continue;} already.push_back(true); Z.unite(u,v); Graph.at(u).push_back({v,w}); Graph.at(v).push_back({u,w}); nowc += w,nowp = max(nowp,p); } if(nowc > C){cout << -1 << endl; return 0;} HLD2 H; vector give = H.make(Graph); SegmentTree S; S.make2(H.n,give); for(int i=0; i C) continue; nowp = p; } cout << nowp << endl; }