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
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x)
#else
#define debug(...) {}
#endif

int main() {
	cin.tie(0)->sync_with_stdio(0);

	mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
	const int magic = 800;
	const LL INF = (1ll << 60);
	int n;
	cin >> n;
	vector<vector<pair<int,int>>> gg(n);
	REP(i,n-1) {
		int a, b, c;
		cin >> a >> b >> c;
		--a;
		--b;
		gg[a].push_back({b, c});
		gg[b].push_back({a, c});
	}
	vector<vector<int>> g(n);
	vector<int> par_cost(n);
	function<void(int,int)> dfs_init = [&](int w, int p) {
		for (auto [b, c] : gg[w]) {
			if (b == p) continue;
			dfs_init(b, w);
			g[w].push_back(b);
			par_cost[b] = c;
		}
		shuffle(g[w].begin(), g[w].end(), rng);
	};
	dfs_init(0, 0);
	vector<vector<LL>> ans(n, vector<LL> (4));
	vector<vector<vector<LL>>> dp(n, vector<vector<LL>>(2));
	vector<int> sh(n);
	function<void(int)> dfs = [&](int w) {
		sh[w] = min(magic, ssize(g[w]) / 2 + 4);
		int p = sh[w];
		int s = 2 * p + 1;
		dp[w] = vector<vector<LL>> (2, vector<LL> (s, -INF));
		dp[w][0][0 + p] = 0;
		for (auto b : g[w]) {
			dfs(b);
			auto dp2 = vector<vector<LL>> (2, vector<LL> (s, -INF));
			REP(j,2) {
				FOR(i,1,s-2) {
					dp2[j][i] = max(dp2[j][i], dp[w][j][i] + ans[b][0]);
					dp2[j][i] = max(dp2[j][i], dp[w][j][i - 1] + ans[b][3]);
					dp2[j][i] = max(dp2[j][i], dp[w][j][i + 1] + ans[b][1]);
					dp2[j][i] = max(dp2[j][i], dp[w][j ^ 1][i] + ans[b][2]);
				}
			}
			dp[w] = dp2;
		}
		ans[w][0] = max(dp[w][0][0 + p], dp[w][0][1 + p] + par_cost[w]);
		ans[w][1] = dp[w][0][0 + p] + par_cost[w];
		ans[w][2] = dp[w][0][-1 + p] + par_cost[w];
		ans[w][3] = dp[w][1][0 + p] + par_cost[w];
		if (w == 0) {
			ans[w][0] = dp[w][0][0 + p];
		}
	};
	dfs(0);
	cout << ans[0][0] << '\n';
}