#include #include using namespace std; using namespace atcoder; //using mint = modint998244353; //const int mod = 998244353; //using mint = modint1000000007; //const int mod = 1000000007; //const int INF = 1e9; //const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define P pair template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } #include using S = long long; S op(S a, S b) { return max(a, b); } S e() { return 0; } struct HeavyLightDecomposition { HeavyLightDecomposition(std::vector> &_g, std::vector&_val) :g(_g), val(_val) { n = g.size(); sz.resize(n); par.resize(n); in.resize(n); out.resize(n); rev.resize(n); head.resize(n); deep.resize(n); } int n; std::vector>g; std::vectorsz, par, in, out, rev, head, deep; std::vectorval; segtreeseg; void build() { dfs_sz(0); int time = 0; dfs_hld(0, time); initSegmentTree(); } void dfs_sz(int v, int d = 0, int p = -1) { deep[v] = d; par[v] = p; sz[v] = 1; if (g[v].size() && (p == g[v][0]))std::swap(g[v][0], g[v].back()); for (auto &e : g[v]) { if (p == e)continue; dfs_sz(e, d + 1, v); sz[v] += sz[e]; if (sz[g[v][0]] < sz[e])std::swap(g[v][0], e); } } void dfs_hld(int v, int &time, int p = -1) { in[v] = time; time++; rev[in[v]] = v; for (auto &e : g[v]) { if (p == e)continue; if (e == g[v][0])head[e] = head[v]; else head[e] = e; dfs_hld(e, time, v); } out[v] = time; } void initSegmentTree() { std::vector_val(n); for (int i = 0; i < n; ++i)_val[i] = val[rev[i]]; seg = segtree(_val); } S query(int u, int v) { S ret = e(); for (;; v = par[head[v]]) { if (in[u] > in[v])std::swap(u, v); if (head[u] == head[v])break; auto get = seg.prod(in[head[v]], in[v] + 1); ret = op(ret, get); } auto get = seg.prod(in[u], in[v] + 1); ret = op(ret, get); return ret; } int la(int v, int k) { while (1) { int u = head[v]; if (in[v] - k >= in[u])return rev[in[v] - k]; k -= in[v] - in[u] + 1; v = par[u]; } } int lca(int u, int v) { for (;; v = par[head[v]]) { if (in[u] > in[v])std::swap(u, v); if (head[u] == head[v])return u; } } int dist(int u, int v) { return deep[u] + deep[v] - 2 * deep[lca(u, v)]; } // point update void update_point(int u, S x) { auto get = seg.get(in[u]); seg.set(in[u], get + 1); } void debug() { for (int i = 0; i < n; ++i) std::cout << seg.prod(i, i + 1) << " "; std::cout << std::endl; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; long long c; cin >> c; struct Edge { int u, v, w, p; bool operator<(const Edge& other)const { return w < other.w; } }; vectore(m); rep(i, m) { cin >> e[i].u >> e[i].v >> e[i].w >> e[i].p; e[i].u--; e[i].v--; } sort(all(e)); vector>to(2 * n - 1); dsu uf(n); int ans = 0; long long mncost = 0; vectorv(n * 2 - 1); int idx = n; for (auto &e : e) { if (uf.same(e.u, e.v))continue; to[e.u].emplace_back(idx); to[idx].emplace_back(e.u); to[idx].emplace_back(e.v); to[e.v].emplace_back(idx); chmax(ans, e.p); mncost += e.w; uf.merge(e.u, e.v); v[idx] = e.w; idx++; } if (mncost > c) { cout << -1 << endl; return 0; } HeavyLightDecomposition hld(to, v); hld.build(); for (auto &e : e) { if (e.p <= ans) continue; auto get = hld.query(e.u, e.v); if (mncost - get + e.w < c)ans = e.p; } cout << ans << endl; return 0; }