#include #include using namespace std; using ll = long long; using pil = pair; template bool chmax(T &a, const T &b){ if (a >= b) return false; a = b; return true; } struct edge { int from, to; ll cost, prof; }; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; ll lim; cin >> lim; vector es(m); for (auto &e : es){ cin >> e.from >> e.to >> e.cost >> e.prof; e.from--, e.to--; } vector ids(m); iota(ids.begin(),ids.end(),0); sort(ids.begin(),ids.end(),[&](int l, int r){ return es[l].cost < es[r].cost; }); atcoder::dsu d(n); ll csum = 0, pmax = 0; vector yet; vector> g(n); for (int i : ids){ auto [u, v, c, p] = es[i]; if (d.same(u,v)){ yet.emplace_back(i); } else { d.merge(u,v); csum += c; chmax(pmax,p); g[u].emplace_back(v,c); g[v].emplace_back(u,c); } } if (csum > lim){ cout << -1 << endl; return 0; } vector> to(20,vector(n,pil(-1,-1))); vector dep(n,0); auto dfs = [&](auto sfs, int v, int f) -> void { for (auto [u, c] : g[v]){ if (u == f) continue; to[0][u] = pil(v,c); dep[u] = dep[v]+1; sfs(sfs,u,v); } }; dfs(dfs,0,-1); for (int i = 1; i < 20; i++){ for (int v = 0; v < n; v++){ if (to[i-1][v].first == -1) continue; auto [u, c] = to[i-1][to[i-1][v].first]; to[i][v] = pil(u,max(c,to[i-1][v].second)); } } auto max_cost = [&](int u, int v){ ll cpath = -1; if (dep[u] > dep[v]) swap(u,v); for (int d = 19; d >= 0; d--){ if ((dep[v]-dep[u]) >> d & 1){ chmax(cpath,to[d][v].second); v = to[d][v].first; } } if (u == v){ return cpath; } for (int d = 19; d >= 0; d--){ if (to[d][u].first == to[d][v].first) continue; chmax(cpath,to[d][u].second); u = to[d][u].first; chmax(cpath,to[d][v].second); v = to[d][v].first; } chmax(cpath,to[0][u].second); chmax(cpath,to[0][v].second); return cpath; }; for (int i : yet){ auto [u, v, c, p] = es[i]; if (pmax >= p) continue; ll cur = csum - max_cost(u,v) + c; if (cur <= lim){ pmax = p; } } cout << pmax << endl; }