#include using namespace std; template bool chmin(T& a, T b) { return a > b ? a = b, true : false; } template bool chmax(T& a, T b) { return a < b ? a = b, true : false; } template concept Iterable = requires(T t) { std::begin(t); std::end(t); }; template requires Iterable && (!is_same_v) ostream& operator<<(ostream& os, const T& container) { for (auto& element : container) os << element << ' '; return os; } template requires ranges::range && (!is_same_v, string>) && (!is_same_v, const char*>) ostream& operator<<(ostream& os, R&& range) { for (auto& element : range)os << element << ' '; return os; } template requires Iterable && (!is_same_v) istream& operator>>(std::istream& is, T& container) { for (auto& e : container)is >> e; return is; } using ll = long long; using ull = unsigned long long; using uint = unsigned int; template struct Edge { int to; T weight; bool operator==(Edge e) { return this->to == e.to and this->weight == e.weight; } bool operator<(Edge e) { return this->to == e.to ? this->weight < e.weight : this->to < e.to; } }; #ifdef _DEBUG #define SHOW(n) {const auto& _ret = n; cerr << #n << ": " << _ret << endl;} #define MSG(x) cerr << x << endl; #else #define SHOW(n) #define MSG(x) #endif //AtCoder Library #include using namespace atcoder; //using mint = modint998244353; using mint = modint1000000007; //using mint1 = dynamic_modint<0>; //using mint = modint; //mint::set_mod(); istream& operator>>(istream& is, mint& x) { ll r; is >> r; x = r; return is; } ostream& operator<<(ostream& os, mint& x) { os << x.val(); return os; } //boost //#include //using namespace boost::multiprecision; //using l3 = int128_t; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector>> g(n); for (int i = 0; i < n - 1; ++i) { int a, b, c; cin >> a >> b >> c; --a, --b; g[a].push_back({ b, c }); g[b].push_back({ a, c }); } ll INF = 1e18; ll res = 0; vector> dp(n); auto dfs = [&](auto&& f, int v, int prev)->void { int deg = g[v].size(); dp[v].resize(deg, -INF); bool update = false; for (int i = 0; i < deg; ++i) { auto [s, w] = g[v][i]; if (s == prev)continue; update = true; f(f, s, v); chmax(dp[v][i], dp[s][0] + w); } ranges::sort(dp[v], ranges::greater()); if (not update)dp[v][0] = 0; }; dfs(dfs, 0, -1); auto dfs2 = [&](auto&& f, int v, int prev, ll pval)->void { int deg = g[v].size(); for (int i = 0; i < deg; ++i) { auto [s, w] = g[v][i]; if (s == prev)dp[v][i] = pval; } ranges::sort(dp[v], ranges::greater()); for (int i = 0; i < deg; ++i) { auto [s, w] = g[v][i]; if (s == prev)continue; ll p = dp[v][0]; f(f, s, v, p); } }; dfs2(dfs2, 0, -1, 0); for (int i = 0; i < n; ++i) { ll r = 0; int deg = g[i].size(); for (int j = 0; j < min(2, deg); ++j) { r += dp[i][j]; } chmax(res, r); } cout << res << endl; return 0; }