#pragma GCC optimize("O2") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define int ll #define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1) #define INT128_MIN (-INT128_MAX - 1) #define clock chrono::steady_clock::now().time_since_epoch().count() #ifdef DEBUG #define dbg(x) cout << (#x) << " = " << x << '\n' #else #define dbg(x) #endif namespace R = std::ranges; namespace V = std::views; using namespace std; using ll = long long; using ull = unsigned long long; using ldb = long double; using pii = pair; using pll = pair; //#define double ldb template ostream& operator<<(ostream& os, const pair pr) { return os << pr.first << ' ' << pr.second; } template ostream& operator<<(ostream& os, const array &arr) { for(const T &X : arr) os << X << ' '; return os; } template ostream& operator<<(ostream& os, const vector &vec) { for(const T &X : vec) os << X << ' '; return os; } template ostream& operator<<(ostream& os, const set &s) { for(const T &x : s) os << x << ' '; return os; } struct DSU { vector bos, sz; int size; DSU(int _size) : bos(_size), sz(_size, 1), size(_size) { iota(bos.begin(), bos.end(), 0); } int query(int v) { if (bos[v] == v) return v; else return bos[v] = query(bos[v]); } bool merge(int v1, int v2) { int b1 = query(v1), b2 = query(v2); if (b1 == b2) return false; if (sz[b1] > sz[b2]) swap(b1, b2); bos[b1] = b2, sz[b2] += sz[b1]; return true; } }; signed main() { ios::sync_with_stdio(false), cin.tie(NULL); int n, k, c; cin >> n >> k >> c; vector> e(k); for(auto &[u, v, w, p] : e) { cin >> u >> v >> w >> p; u--, v--; } R::sort(e, less{}, [](auto x) { return x[2]; }); vector inT(k, false); vector teid; int cost = 0, price = 0; { DSU dsu(n); for(int i = 0; auto [u, v, w, p] : e) { if (dsu.merge(u, v)) { inT[i] = true, cost += w, price = max(price, p); teid.emplace_back(i); } i++; } } if (cost > c) { cout << -1 << '\n'; return 0; } vector> bs; for(int i = 0; i < k; i++) if (!inT[i]) bs.push_back({0, n - 2, i}); while(!bs.empty()) { DSU dsu(n); sort(bs.begin(), bs.end(), [](auto x, auto y) { return (x[0] + x[1]) / 2 < (y[0] + y[1]) / 2; }); vector> nxt; int ptr = 0; for(auto [l, r, i] : bs) { if (l == r) { if (int tmp = cost - e[teid[l]][2] + e[i][2]; tmp <= c) price = max(price, e[i][3]); continue; } int mid = (l + r) / 2; while(ptr <= mid) { int k = teid[ptr++]; dsu.merge(e[k][0], e[k][1]); } if (dsu.query(e[i][0]) == dsu.query(e[i][1])) nxt.push_back({l, mid, i}); else nxt.push_back({mid + 1, r, i}); } bs.swap(nxt); } cout << price << '\n'; return 0; }