Problem:
Diameter of a Tree

Given a tree T with non-negative weight, find the diameter of the tree.

The diameter of a tree is the maximum distance between two nodes in a tree.

Input

n
s1 t1 w1
s2 t2 w2
:
sn-1 tn-1 wn-1

The first line consists of an integer n which represents the number of nodes in the tree. Every node has a unique ID from 0 to n-1 respectively.

In the following n-1 lines, edges of the tree are given.

si and ti represent end-points of the i-th edge (undirected) and wi represents the weight (distance) of the i-th edge.

Output

Print the diameter of the tree in a line.

Constraints

1 ≤ n ≤ 100,000

0 ≤ wi ≤ 1,000

Sample Input 1

4
0 1 2
1 2 1
1 3 3

Sample Output 1

5

Sample Input 2

4
0 1 1
1 2 2
2 3 4

Sample Output 2

7

Solution in C++14 (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
// typedef __int128_t Int;
#define DBG 1
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define loop(n) rep(loop, (0), (n))
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
struct Edge {
  int source, target, weight;
  Edge(int source, int target, int weight)
      : source(source), target(target), weight(weight) {}
};
struct Graph {
  int V;
  vector<vector<Edge>> g;
  Graph(int V) : V(V), g(V) {}
  void add_edge(int s, int t, int w = 1) { g[s].push_back(Edge(s, t, w)); }
  void dfs(vector<int> &x, int v, int p, int d) {
    x[v] = d;
    rep(i, 0, g[v].size()) {
      Edge e = g[v][i];
      int u = e.target;
      if (u == p)
        continue;
      dfs(x, u, v, d + e.weight);
    }
  }
  int diameter() {
    vector<int> d(V);
    dfs(d, 0, -1, 0);
    int m = max_element(all(d)) - d.begin();
    dfs(d, m, -1, 0);
    return *max_element(all(d));
  }
};
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);

  int N;
  cin >> N;
  Graph g(N);
  rep(i, 0, N - 1) {
    int s, t, w;
    cin >> s >> t >> w;
    g.add_edge(s, t, w);
    g.add_edge(t, s, w);
  }
  cout << g.diameter() << endl;
  return 0;
}