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
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
typedef long long lld;
typedef pair<lld, lld> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back

constexpr size_t N = 1 << 19;
constexpr lld neg_inf = -(1ll << 56);
vector<pll> g[N];
lld dp[4][N];
int n;

/*int my_random(int lim) {
	static random_device rd;
	static mt19937 rng{rd()};
	return uniform_int_distribution<int>(0, lim - 1)(rng);
}

void my_input() {
	n = 200001;
	int nn = n >> 2;
	lld a, b, c = 1000;
	int i = 2;
	for (; i <= n - nn;) {
		g[i].pb(mp(1, c));
		g[1].pb(mp(i, c));
		++i;
		g[i].pb(mp((i - 1), c));
		g[(i - 1)].pb(mp(i, c));
		++i;
		g[i].pb(mp((i - 1), c));
		g[(i - 1)].pb(mp(i, c));
		++i;
	}
	
	for (; i <= n; ++i) {
		g[i].pb(mp(1, c));
		g[1].pb(mp(i, c));
	}
	
	for (int i = 1; i <= n; ++i) {
		random_shuffle(g[i].begin(), g[i].end(), my_random);
	}
} */

void read_input() {
	int a, b;
	lld c;
	scanf("%d", &n);
	for (int i = 1; i < n; ++i) {
		scanf("%d%d%lld", &a, &b, &c);
		g[a].pb(mp(b, c));
		g[b].pb(mp(a, c));
	}
	
	for (int i = 1; i <= n; ++i) {
		random_shuffle(g[i].begin(), g[i].end());
	}
}

lld dp2[2][2][N + N]; // dp2[nr_sasiada][(#2) & 1][#1 - #3]

void dfs(int v, int y) {
	for (auto x : g[v]) {
		if (x.ff == y)
			continue;
			
		dfs(x.ff, v);
	}
	
	lld x, ed, a, b, c, d;
	
	//printf("liczę %d:\n", v);
	
	for (size_t k = 1; k <= g[v].size(); ++k) {
		dp2[0][0][N - k] = neg_inf;
		dp2[0][0][N + k] = neg_inf;
		dp2[0][1][N - k] = neg_inf;
		dp2[0][1][N + k] = neg_inf;
		dp2[1][0][N - k] = neg_inf;
		dp2[1][0][N + k] = neg_inf;
		dp2[1][1][N - k] = neg_inf;
		dp2[1][1][N + k] = neg_inf;
	}

	dp2[0][0][N] = 0;
	dp2[1][0][N] = neg_inf;
	dp2[0][1][N] = neg_inf;
	dp2[1][1][N] = neg_inf;
	
	size_t q = 0, p = 1;
	
	for (size_t k = 0; k < g[v].size(); ++k) {
		x = g[v][k].ff;
		if (x == y)
			continue;

		ed = g[v][k].ss;
		a = max(dp[0][x], dp[3][x] + ed); 	// 0
		b = dp[0][x] + ed;	// 1
		c = dp[1][x] + ed;	// 2
		d = dp[2][x] + ed;	// 3
		
		//printf("x: %lld, a: %lld, b: %lld, c:%lld, d: %lld\n", x, a, b, c, d);
			
		for (size_t i = max(N - k, N - 800); i <= min(N + k, N + 800); ++i) {
			//printf("dp2[%lu][0][%lld] = %lld, dp2[%lu][1][%lld] = %lld\n", q, i - N, dp2[q][0][i], q, i - N, dp2[q][0][i]);
			dp2[p][0][i] = max(dp2[p][0][i], dp2[q][0][i] + a);
			dp2[p][1][i] = max(dp2[p][1][i], dp2[q][0][i] + c);
			dp2[p][0][i + 1] = max(dp2[p][0][i + 1], dp2[q][0][i] + b);
			dp2[p][0][i - 1] = max(dp2[p][0][i - 1], dp2[q][0][i] + d);
			
			dp2[p][1][i] = max(dp2[p][1][i], dp2[q][1][i] + a);
			dp2[p][0][i] = max(dp2[p][0][i], dp2[q][1][i] + c);
			dp2[p][1][i + 1] = max(dp2[p][1][i + 1], dp2[q][1][i] + b);
			dp2[p][1][i - 1] = max(dp2[p][1][i - 1], dp2[q][1][i] + d);
			
			dp2[q][0][i] = neg_inf;
			dp2[q][1][i] = neg_inf;
		}
		
		//printf("po %lu-tym kroku: (0)%lld (1)%lld (2)%lld (3)%lld\n", k, dp2[p][0][N], dp2[p][0][N + 1], dp2[p][1][N], dp2[p][0][N - 1]);
		
		q ^= 1, p ^= 1;
	}
	
	dp[0][v] = dp2[q][0][N];
	dp[1][v] = dp2[q][0][N + 1];
	dp[2][v] = dp2[q][1][N];
	dp[3][v] = dp2[q][0][N - 1];
	//printf("wychodzę z %d: (0)%lld (1)%lld (2)%lld (3)%lld\n", v, dp[0][v], dp[1][v], dp[2][v], dp[3][v]);
}

int main(){
	srand(694202137);
	read_input();
	//my_input();
	
	dfs(1, -1);
	
	printf("%lld\n", dp[0][1]);
	
	return 0;
}
 /*
 
19
1 2 1
2 3 2
3 4 -1
4 5 -1
5 6 2
6 7 11
7 8 12
8 9 13
9 10 14
11 12 3
12 13 0
13 14 0
14 15 0
15 16 1
16 4 0
4 17 0
17 18 0
18 19 2
 
*/