#include #include using namespace std; using namespace atcoder; struct Fast { Fast() { std::cin.tie(nullptr); ios::sync_with_stdio(false); cout << setprecision(10); } } fast; #define all(a) (a).begin(), (a).end() #define contains(a, x) ((a).find(x) != (a).end()) #define rep(i, a, b) for (int i = (a); i < (int)(b); i++) #define rrep(i, a, b) for (int i = (int)(b)-1; i >= (a); i--) #define YN(b) cout << ((b) ? "YES" : "NO") << "\n"; #define Yn(b) cout << ((b) ? "Yes" : "No") << "\n"; #define yn(b) cout << ((b) ? "yes" : "no") << "\n"; template ostream &operator<<(ostream &os, vector &vec) { for (int i = 0; i < vec.size(); i++) { os << vec[i] << (i + 1 == vec.size() ? "" : " "); } return os; } template inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } using ll = long long; using vb = vector; using vvb = vector; using vi = vector; using vvi = vector; using vl = vector; using vvl = vector; using mint = modint998244353; using vm = vector; using vvm = vector; inline ostream &operator<<(ostream &os, const mint x) { return os << x.val(); }; void solve() { int n, k; ll c; cin >> n >> k >> c; vi u(k), v(k), w(k), p(k), idx(k); rep(i, 0, k) { cin >> u[i] >> v[i] >> w[i] >> p[i]; u[i]--; v[i]--; idx[i] = i; } sort(all(idx), [&w](auto const &lhs, auto const &rhs) { return w[lhs] < w[rhs]; }); ll cost = 0; { dsu uf(n); for (auto i : idx) if (!uf.same(u[i], v[i])) { uf.merge(u[i], v[i]); cost += w[i]; } if (uf.size(0) != n || cost > c) { cout << -1 << "\n"; return; } } vi l(k, -1), r(k, k); vector> st(k); rep(loop, 0, 18) { rep(i, 0, k) { if (r[i] - l[i] > 1) { int x = l[i] + (r[i] - l[i]) / 2; st[x].push(i); } } dsu uf(n); rep(t, 0, k) { int x = idx[t]; uf.merge(u[x], v[x]); while (!st[t].empty()) { int i = st[t].top(); st[t].pop(); if (uf.same(u[i], v[i])) r[i] = t; else l[i] = t; } } } int ans = 0; rep(i, 0, k) if (cost - w[idx[r[i]]] + w[i] <= c) chmax(ans, p[i]); cout << ans << "\n"; } int main() { int t = 1; // cin >> t; while (t--) solve(); }