#include #include #include #include using namespace std; using ll = long long; using ld = long double; constexpr int INF = (1 << 30) - 10; constexpr ll LINF = 1LL << 60; inline void output_YesNo(bool x) { cout << (x ? "Yes" : "No") << endl; } template inline void chmax(T &lhs, const T &rhs) { lhs = max(lhs, rhs); } template inline void chmin(T &lhs, const T &rhs) { lhs = min(lhs, rhs); } struct edge { int to; ll cost; }; struct status { ll value; int prev; int now; bool operator<(const status &rhs) const { return value < rhs.value; }; bool operator>(const status &rhs) const { return value > rhs.value; }; }; vector dijkstra(const int s, const vector > &graph) { priority_queue, greater<> > que; vector dis(graph.size(), LINF); dis[s] = 0; que.push({0, -1, s}); while (!que.empty()) { const auto [value, prev, v] = que.top(); que.pop(); if (dis[v] < value)continue; for (const auto [to, cost]: graph[v]) { if (to == prev) continue; if (dis[to] > value + cost) { dis[to] = value + cost; que.push({dis[to], v, to}); } } } return dis; } int main() { int n; cin >> n; vector > graph(n); for (int i = 0; i < n - 1; i++) { int from, to, cost; cin >> from >> to >> cost; from--, to--; graph[from].push_back({to, cost}); graph[to].push_back({from, cost}); } vector dis = dijkstra(0, graph); int farV = -1; ll farDis = -LINF; for (int i = 0; i < n; i++) { if (farDis < dis[i]) farV = i, farDis = dis[i]; } dis = dijkstra(farV, graph); farDis = -LINF; for (int i = 0; i < n; i++) { if (farDis < dis[i]) farV = i, farDis = dis[i]; } cout << farDis << endl; return 0; }