1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ll long long
#define ull unsigned long long
//#define int LL
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define VI vector<int>
#define VVI vector<VI>
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define st first
#define nd second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)x.size())

template<class C> void mini(C& x, C y) { x = min(x,y); }
template<class C> void maxi(C& x, C y) { x = max(x,y); }

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
	while(*sdbg!=',') cerr<<*sdbg++;
	cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream& os, vector<T> V) {
	os << "["; for (auto vv : V) os << vv << ","; return os << "]";
}
template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
	return os << "(" << P.st << "," << P.nd << ")";
}

#ifdef ONLINE_JUDGE
#undef LOCAL
#endif

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

const int MAXN = 2e5+10;
const int MAXM = 3 * MAXN;

struct Edge {
	int v;
	ll x;
	int len;
	int id;
	Edge() = default;
	Edge(int _v, ll _x, int _len, int _id) : v(_v), x(_x), len(_len), id(_id) {}
};

struct Tree {
	int n;
	vector<Edge> adj[MAXN * 2];
	
	vector<Edge>& operator[] (unsigned int i) {
		return adj[i];
	}

	void AddEdge(int u, int v, ll x, int len, int id) {
		adj[u].eb(v, x, len, id);
		adj[v].eb(u, x, len, id);
	}
};

int n, m, edgeid;
Tree F, G;

void ToBinaryTree(int u, int fa=0) {
	int last = u;
	for(const auto& e : F[u]) {
		if(e.v != fa) {
			G.AddEdge(last, ++G.n, 0, 0, ++edgeid);
			G.AddEdge(G.n, e.v, e.x, 1, ++edgeid);
			last = G.n;
			ToBinaryTree(e.v, u);
		}
	}
}

bool disable[MAXM];
int siz[MAXN];
Edge E[MAXM];

void GetSize(int u, int fa=0) {
	siz[u] = 1;
	for(const auto& e : G[u]) {
		if(e.v != fa and !disable[e.id]) {
			E[e.v] = Edge(u, e.x, e.len, e.id);
			GetSize(e.v, u);
			siz[u] += siz[e.v];
		}
	}
}

int FindCentroid(int u, int lim, int fa = 0) {
	if(siz[u] <= lim) {
		return u;
	}
	int mxsiz = 0,id;
	for(const auto& e : G[u]) {
		if(e.v != fa and !disable[e.id]) {
			int tmp = FindCentroid(e.v, lim, u);
			if(siz[tmp] > mxsiz) {
				mxsiz = siz[tmp];
				id = tmp;
			}
		}
	}
	return id;
}

const ll inf = 1e16;

ll CentroidDecomposition(int node) {
	GetSize(node);
	if(siz[node] <= 1) return 0;
	
	int centroid1 = FindCentroid(node, siz[node]/2);
	int centroid2 = E[centroid1].v;

	int eid = E[centroid1].id;
	int elen = E[centroid1].len;
	int ex = E[centroid1].x; 

	vector<ll> best[2] = {{0, -inf, -inf, -inf, -inf}, {0, -inf, -inf, -inf, -inf}};

	function<void(int,int,ll,Edge,bool)> dfs = [&](int u, int len, ll sum, Edge parEdge, bool flag) {
		disable[parEdge.id] = true;
		sum += parEdge.x;
		len += parEdge.len;

		if(parEdge.len > 0) {
			maxi(best[flag][len], CentroidDecomposition(u) + sum);
			if(len == 4 - elen) {
				disable[parEdge.id] = false;
				return;
			}
		}

		for(Edge e : G[u]) {
			if(disable[e.id]) continue;
			dfs(e.v, len, sum, e, flag);
		}

		if(parEdge.id != -1) disable[parEdge.id] = false;
	};

	dfs(centroid1, -1, 0, Edge(centroid1,0,1, eid), true);
	dfs(centroid2, -1, 0, Edge(centroid2,0,1, eid), false);

	ll res = 0;
	maxi(res, best[0][0] + best[1][0]);

	if(elen == 1) {
		maxi(res, best[0][0] + best[1][3] + ex);
		maxi(res, best[0][1] + best[1][2] + ex);
		maxi(res, best[0][2] + best[1][1] + ex);
		maxi(res, best[0][3] + best[1][0] + ex);
	}

	else {
		maxi(res, best[0][4] + best[1][0]);
		maxi(res, best[0][3] + best[1][1]);
		maxi(res, best[0][2] + best[1][2]);
		maxi(res, best[0][1] + best[1][3]);
		maxi(res, best[0][0] + best[1][4]);
	}

	disable[eid] = false;

	return res;
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr);

	cin >> n;
	REP(i, n-1) {
		int u, v, x;
		cin >> u >> v >> x;
		F.AddEdge(u,v,x,1,i);
	}

	G.n=n;
	F.n=n;
	ToBinaryTree(1);

	cout << CentroidDecomposition(1);

	return 0;
}