#include #include #include #include #include #include #include #include #include #include #include int main() { int n, m; std::cin >> n >> m; std::vector< std::vector > > edges(n); for (int i = 0; i < m; i++) { int a, b; int64_t w; std::cin >> a >> b >> w; edges[a - 1].push_back({ b - 1,w }); edges[b - 1].push_back({ a - 1,w }); } std::vector > mst(n); std::vector parent(n); std::vector parentweight(n); std::vector visited(n); struct Event { int prev; int vert; int64_t cost; bool operator<(const Event& other) const { return cost > other.cost; } }; std::priority_queue pq; pq.push({ -1, 0, 0 }); while (!pq.empty()) { auto next = pq.top(); pq.pop(); if (visited[next.vert]) continue; visited[next.vert] = true; if (next.prev != -1) { int v1 = next.prev; int v2 = next.vert; mst[v1].push_back(v2); parent[v2] = v1; parentweight[v2] = next.cost; } for (auto it : edges[next.vert]) { if (!visited[it.first]) { pq.push({ next.vert, it.first, it.second }); } } } std::vector loidx(n); std::vector hiidx(n); int curidx = 1; std::deque q; q.push_back(0); while (!q.empty()) { int next = q.front(); q.pop_front(); if (loidx[next] == 0) { loidx[next] = curidx++; q.push_front(next); for (auto it : mst[next]) q.push_front(it); } else { hiidx[next] = curidx++; } } std::vector lift(20 * n); for (int i = 0; i < n; i++) { lift[20 * i] = parent[i]; } for (int i = 0; i < 19; i++) { for (int j = 0; j < n; j++) { lift[20 * j + i + 1] = lift[20 * lift[20 * j + i] + i]; } } std::vector liftvals(20 * n); for (int i = 0; i < 20 * n; i++) { liftvals[i] = std::numeric_limits::max(); } for (int i = 0; i < n; i++) { for (auto it : edges[i]) { if (it.first < i) continue; if (parent[it.first] == i || parent[i] == it.first) continue; int v1 = i; int v2 = it.first; int64_t w = it.second; int lc = 19; int cur = v1; while (loidx[cur] > loidx[v2] || hiidx[cur] < loidx[v2]) { while (lc > 0 && loidx[lift[20 * cur + lc]] <= loidx[v2] && loidx[v2] <= hiidx[lift[20 * cur + lc]]) lc--; liftvals[20 * cur + lc] = std::min(liftvals[20 * cur + lc], w); cur = lift[20 * cur + lc]; } lc = 19; cur = v2; while (loidx[cur] > loidx[v1] || hiidx[cur] < loidx[v1]) { while (lc > 0 && loidx[lift[20 * cur + lc]] <= loidx[v1] && loidx[v1] <= hiidx[lift[20 * cur + lc]]) lc--; liftvals[20 * cur + lc] = std::min(liftvals[20 * cur + lc], w); cur = lift[20 * cur + lc]; } } } for (int i = 18; i >= 0; i--) { for (int j = 0; j < n; j++) { liftvals[20 * j + i] = std::min(liftvals[20 * j + i], liftvals[20 * j + i + 1]); liftvals[20 * lift[20 * j + i] + i] = std::min(liftvals[20 * lift[20 * j + i] + i], liftvals[20 * j + i + 1]); } } int64_t totweight = 0; for (int i = 1; i < n; i++) { totweight += parentweight[i]; } int64_t answer = 0; for (int i = 1; i < n; i++) answer = std::max(answer, totweight - parentweight[i] + liftvals[20 * i]); std::cout << answer << std::endl; }