#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; struct HLD { int root, n; vector sz, parent, depth, idx, ridx, head, inv; HLD(const auto& to, int root=0) : root(root), n(to.size()), sz(n), parent(n), depth(n), idx(n), ridx(n), head(n), inv(n) { init_tree_data(to, root, -1, 0); int nxt = 0; assign_idx(to, root, root, nxt); } void init_tree_data(const auto& to, int u, int p, int d) { parent[u] = p; depth[u] = d; int s = 1; for (auto [v, _]: to[u]) if (v != p) { init_tree_data(to, v, u, d+1); s += sz[v]; } sz[u] = s; } void assign_idx(const auto& to, int u, int h, int& nxt, int p=-1) { head[u] = h; idx[u] = nxt; inv[nxt] = u; nxt++; int heaviest = -1; int mxweight = 0; for (auto [v, _]: to[u]) if (v != p) { if (sz[v] > mxweight) { heaviest = v; mxweight = sz[v]; } } if (heaviest != -1) { assign_idx(to, heaviest, h, nxt, u); for (auto [v, _]: to[u]) if (v != p && v != heaviest) { assign_idx(to, v, v, nxt, u); } } ridx[u] = nxt; } int lca(int u, int v) { while (head[u] != head[v]) { if (depth[head[u]] > depth[head[v]]) { u = parent[head[u]]; } else { v = parent[head[v]]; } } return depth[u] < depth[v] ? u : v; } // returns reference to tuple of (path fragments from x upto lca (excluding lca), those from y, lca) // storage of retval is reused to avoid creating short vectors on each query tuple>, vector>, int> paths_res; auto& paths(int x, int y) { auto& [x_paths, y_paths, lca] = paths_res; x_paths.clear(); y_paths.clear(); while (head[x] != head[y]) { int hx = head[x], hy = head[y]; if (depth[hx] > depth[hy]) { x_paths.emplace_back(x, hx); x = parent[hx]; } else { y_paths.emplace_back(y, hy); y = parent[hy]; } } if (depth[x] > depth[y]) { x_paths.emplace_back(x, inv[idx[y] + 1]); x = y; } else if (depth[x] < depth[y]) { y_paths.emplace_back(y, inv[idx[x] + 1]); y = x; } lca = x; return paths_res; } int dist(int u, int v) { int w = lca(u, v); return depth[u] + depth[v] - 2 * depth[w]; } template int max_ancestor(int v, F f) { if (!f(v)) return -1; int hv = head[v]; int p = parent[hv]; while (p != -1 && f(p)) { v = p; hv = head[v]; p = parent[hv]; } int il = idx[hv] - 1, ir = idx[v]; while (ir - il > 1) { int ic = (il + ir) / 2; (f(inv[ic]) ? ir : il) = ic; } return inv[ir]; } int ascend(int v, int k) { assert(depth[v] >= k); int td = depth[v] - k; int hv = head[v]; while (depth[hv] > td) { v = parent[hv]; hv = head[v]; } int rest = depth[v] - td; return inv[idx[v] - rest]; } int move_to(int u, int v, int by) { int l = lca(u, v); int du = depth[u] - depth[l]; int dv = depth[v] - depth[l]; assert(by <= du + dv); if (by <= du) return ascend(u, by); else return ascend(v, du + dv - by); } }; template struct DisjointSparseTable { const int n; const vector msb; const vector> d; DisjointSparseTable(vector a) : n(a.size()), msb(build_msb_table(n)), d(build_table(move(a))) {} vector build_msb_table(int n) { if (n <= 1) return {}; unsigned char k_max = 32 - __builtin_clz(n - 1); vector res(1 << k_max); unsigned char* p = res.data(); for (unsigned char k = 0; k < k_max; k++) { memset(p + (1U << k), k, 1U << k); } return res; } vector> build_table(vector a) { const int n = a.size(); vector> res(1); if (n >= 2) { const int i_max = n - 1, k_max = 32 - __builtin_clz(i_max); res.resize(k_max); for (int k = 1; k < k_max; k++) { vector t(i_max >> k & 1 ? n : i_max & ~0U << k); for (int c = 1 << k; c < n; c += 1 << (k + 1)) { int l = c - (1 << k); int r = min(n, c + (1 << k)); t[c - 1] = a[c - 1]; for (int i = c - 2; i >= l; i--) t[i] = op(a[i], t[i + 1]); t[c] = a[c]; for (int i = c + 1; i < r; i++) t[i] = op(t[i - 1], a[i]); } res[k] = move(t); } } res[0] = move(a); return res; } S query(int l, int r) { return query_closed(l, r - 1); } S query_closed(int l, int r) { assert(0 <= l && l <= r && r < n); if (l == r) return d[0][l]; auto k = msb[l ^ r]; return op(d[k][l], d[k][r]); } }; int op(int x, int y) { return max(x, y); } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; ll c; cin >> n >> m >> c; struct E { int u, v, w, p; }; vector es(m); for (auto& [u, v, w, p] : es) { cin >> u >> v >> w >> p; u--, v--; } ranges::sort(es, {}, &E::w); vector> to(n); dsu uf(n); int ans = 0; ll min_cost = 0; for (auto [u, v, w, p] : es) { if (uf.same(u, v)) continue; to[u].emplace_back(v, w); to[v].emplace_back(u, w); chmax(ans, p); min_cost += w; uf.merge(u, v); } if (min_cost > c) { cout << -1 << '\n'; return 0; } HLD hld(to); VI init(n); rep(u, n) if (u) { for (auto [v, w] : to[u]) if (v == hld.parent[u]) { init[hld.idx[u]] = w; break; } } DisjointSparseTable dst(init); for (auto [u, v, w, p] : es) if (p > ans) { auto& [p1, p2, lca] = hld.paths(u, v); int cmx = 0; rep(_, 2) { for (auto [x, y] : p1) { int ix = hld.idx[x], iy = hld.idx[y]; chmax(cmx, dst.query_closed(iy, ix)); } swap(p1, p2); } if (min_cost - cmx + w <= c) ans = p; } cout << ans << '\n'; }