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
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef double K;
// constexpr int INF = 0x3f3f3f3f;

#define FOR(i, b, e) for(int i = (b); i < (e); i++)
#define TRAV(x, a) for(auto &x: (a))
#define SZ(x) ((int)(x).size())
#define PB push_back
#define X first
#define Y second

constexpr int N = 2e5 + 5;
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;

vector<ii> G[N];
ll wyn[N][4];

constexpr int OFF = 1600;
ll dp[OFF * 2][2];
ll nw[OFF * 2][2];

void dfs(int v, int p) {
	TRAV(x, G[v]) if(x.X != p) dfs(x.X, v);
	int r = min((int)(3.5 * sqrt(SZ(G[v]))) + 10, (SZ(G[v]) + 1) / 2);
	for(int i = OFF - r - 1; i <= OFF + r + 1; i++) dp[i][0] = dp[i][1] = -INF;
	dp[OFF][0] = 0;
	TRAV(x, G[v]) {
		if(x.X == p) continue;
		ll zer = max(wyn[x.X][0], wyn[x.X][3] + x.Y);
		ll jed = wyn[x.X][0] + x.Y;
		ll dwa = wyn[x.X][1] + x.Y;
		ll trz = wyn[x.X][2] + x.Y;
		for(int i = OFF - r; i <= OFF + r; i++) {
			nw[i][0] = max({dp[i][0] + zer, dp[i + 1][0] + jed,
							dp[i][1] + dwa, dp[i - 1][0] + trz});
			nw[i][1] = max({dp[i][1] + zer, dp[i + 1][1] + jed,
							dp[i][0] + dwa, dp[i - 1][1] + trz});
		}
		for(int i = OFF - r; i <= OFF + r; i++) dp[i][0] = nw[i][0], dp[i][1] = nw[i][1];
	}
	wyn[v][0] = dp[OFF][0], wyn[v][1] = dp[OFF - 1][0];
	wyn[v][2] = dp[OFF][1], wyn[v][3] = dp[OFF + 1][0];
}

void solve() {
	int n;
	cin >> n;
	FOR(i, 1, n) {
		int a, b, c;
		cin >> a >> b >> c;
		G[a - 1].PB({b - 1, c});
		G[b - 1].PB({a - 1, c});
	}
	mt19937 g(325818513);
	FOR(i, 0, n) shuffle(G[i].begin(), G[i].end(), g);
	dfs(n / 17, n / 17);
	cout << wyn[n / 17][0] << '\n';
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	// int tt; cin >> tt;
	// FOR(te, 0, tt) {
	// 	// cout << "Case #" << te + 1 << ": ";
	// 	solve();
	// }
	solve();
	return 0;
}