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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#pragma GCC target("sse,sse2,sse3,mmx,abm,tune=native") // for szkopul and sio only
typedef long long lld;
typedef double lf;
typedef long double llf;
typedef pair<int,int> pii;
typedef pair<lld,lld> pll;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
#define For(i,s,a) for(lld i = (lld)s; i < (lld)a; ++i)
#define rpt(s, it) for(auto it = s.begin(); it != s.end(); ++it)
#define brpt(s, it) for(auto it = s.rend(); it != s.rbegin(); --it)
#define pb push_back
#define eb emplace_back
#define ff first
#define dd second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define make_unique(x) (x).erase( unique(all(x)), (x).end())
#define popcnt(x) __builtin_popcount(x)
#define sz size()
#define time_since duration_cast< milliseconds >(system_clock::now().time_since_epoch())
 
template<typename Ta, typename Tb>
ostream & operator <<(ostream & os, pair<Ta, Tb> x){
	return os << x.ff << " " << x.dd;
}

const int N = 2e5 + 1;
lld dp[5][N];
vector<pii>c[N];
const lld inf = 1e16;
int hei[N];
lld buff[5];
//const lld inf = 1000;
lld banned;
pii banned_edge = {-1, -1};

#define fmax(a, b) (a) > (b) ? (a) : (b)

bool debug = 0;
int X, P;
lld mask1, mask2, mask3, mask4, mask0;

lld chk() {
	lld cand = 0;	
	//if(debug)
	//	cout << X << " " << P << " " << mask1 << " " << mask2 << " " << mask3 << " " << mask4 << " " << mask0 << endl;
	For(i, c[X][0].ff == P, c[X].sz) {
		lld k = 1ll << (i - (c[X][0].ff == P));
	//	if(debug)
	//		cout << i << " " << k << endl;
		if(k & mask0)
			cand += dp[0][c[X][i].ff];
		else
			cand += c[X][i].dd;
		if(k & mask1)
			cand += dp[0][c[X][i].ff];
		if(k & mask2)
			cand += dp[1][c[X][i].ff];
		if(k & mask3)
			cand += dp[2][c[X][i].ff];
		if(k & mask4)
			cand += dp[3][c[X][i].ff];
	}
	//if(debug)
	//	cout << cand << endl;
	return cand;
}

lld solve1(lld mask) {
	//cout << x << " " << mask << endl;
	lld ret = -inf;
	for(mask1 = mask; mask1; mask1 = (mask1 - 1) & mask) {
		if(popcnt(mask1) != popcnt(mask - mask1))
			continue;
		mask3 = mask - mask1;
	//	cout << x << " " << p << " " << mask0 << " " << mask1 << " " << mask2 << " " << mask << " " << mask4 << endl;
		ret = max(ret, chk());
	}
	if(mask == 0) {
		mask1 = mask3 = 0;
		ret = max(ret, chk());
	}
	return ret;
}

lld solve2(lld mask) {
	if(banned && (banned & mask))
		return -inf;
	lld ret = -inf;
	for(mask2 = mask; mask2; mask2 = (mask2 - 1) & mask) {
		if(__builtin_popcountll(mask2) & 1)
			continue;
		ret = max(ret, solve1(mask - mask2));
	}
	mask2 = 0;
	ret = max(ret, solve1(mask));
	return ret;
}

lld solve4(lld mask) {
	//cout << x << endl;
	lld ret = -inf;
	for(mask4 = mask; mask4; mask4 = (mask4 - 1) & mask) {
		if(popcnt(mask - mask4) & 1)
			continue;
		//cout << x << " " << p << " " << mask << " " << mask4 << " " << mask0 << endl;
		ret = max(ret, solve2(mask - mask4));
	}
	if(!(popcnt(mask) & 1)) {
		mask4 = 0;
		ret = max(ret, solve2(mask));
	}
	return ret;
}

lld oldbrut(lld x, lld p, lld failsafe) {
	if(hei[x] < 2)
		return 0;
	lld xd = c[x].sz - (c[x][0].ff == p);
	//cout << xd << endl;
	lld pot = 1;
	pot = 1ll << xd;
	lld ret = -inf;
	X = x;
	P = p;
	if(xd < 11) {
		for(mask0 = 0; mask0 < pot; ++mask0) {
			// wybieram zera
			//for(lld mask0 = mask; mask0; mask0 = (mask0 - 1) & mask) {
				//cout << mask0 << " " << mask << endl;
			//	solve4(x, p, mask - mask0, mask0);
			//}
			//if(banned && (banned & mask))
			//	continue;
			ret = max(ret, solve4(pot - mask0 - 1));
		}
	} else {
		ret = 0;//failsafe;
	}
	return ret;
}

#define fairly_much 245
lld jajco[2][512][256];
lld mybuff[N];

void dpset(int l, int b, int d, lld w, bool force = 1) {
	if(force)
		jajco[l][b + fairly_much][d] = w;
	else
		jajco[l][b + fairly_much][d] = fmax(jajco[l][b + fairly_much][d], w);
}

lld get(int l, int b, int d) {
	return jajco[l][b + fairly_much][d];
}

lld brut(int x, int p, lld failsafe) {
	if(hei[x] < 2)
		return 0;
	bool important = c[x][0].ff == p;
	int dpn = c[x].sz - (c[x][0].ff == p);
	
	lld wtf = 0;
	For(i, c[x][0].ff == p, c[x].sz) {
		mybuff[i - important] = fmax(dp[0][c[x][i].ff], dp[3][c[x][i].ff] + (c[x][i].ff == banned_edge.dd ? -inf : c[x][i].dd));
		wtf += mybuff[i - important];
	}
	
	if(dpn > 240)
		return fmax(failsafe, wtf);
		
	For(d, 0, 2)
	For(i, fairly_much - dpn - 3, fairly_much + dpn + 3)
	For(k, 0, dpn + 1)
		jajco[d][i][k] = -inf;
		
	dpset(0, 0, 0, wtf);
	//if(x == 1)	
	//	cout << wtf << " xddd " << banned_edge << endl;
	
	For(l, 1, dpn + 1) {
		int th = (l & 1), prv = (l & 1) ^ 1;
		int v = c[x][l - 1 + important].ff;
		lld edge = c[x][l - 1 + important].dd;
		if(v == banned_edge.dd)
			edge = -inf;
	//	if(x == 1)
	//		cout << v << " wtf " << edge << " " << mybuff[l - 1] << endl;
		For(d, 0, l + 1)
		For(b, -(l - d), l - d + 1) {
			dpset(th, b, d, -inf);
			dpset(th, b, d, get(prv, b, d), 0);
			dpset(th, b, d, get(prv, b - 1, d) + dp[0][v] + edge - mybuff[l - 1], 0);
			dpset(th, b, d, get(prv, b + 1, d) + dp[2][v] + edge - mybuff[l - 1], 0);
			if(d)
				dpset(th, b, d, get(prv, b, d - 1) + dp[1][v] + edge - mybuff[l - 1], 0);
	//		if(get(th, b, d) > 0 && x == 1)
	//			cout << get(th, b, d) << " " << b << " " << d << " " << v << " " << l << endl;
		}
	}
	
	lld ret = 0;
	for(int k = 0; k <= dpn; k += 2) 
		ret = fmax(ret, get(dpn & 1, 0, k));
	//if(x == 1)
	//	cout << "ret: " << ret << endl;
	
	return ret;
	
}

void dfbuild(int x, int p = -1) {
	banned = -1;
	hei[x] = 0;
	lld dpsub = 0;
	For(i, 0, c[x].sz)
		if(c[x][i].ff == p) {
			swap(c[x][i], c[x][0]);
			break;
		}
	
	for(auto s : c[x])
		if(s.ff != p) {
			dfbuild(s.ff, x); 
			hei[x] = max(hei[s.ff] + 1, hei[x]); 
			dpsub += dp[0][s.ff];
		}
		
	dp[0][x] = max(dpsub, 0ll);
	dp[0][x] = max(dp[0][x], brut(x, p, dpsub));
	
	For(h, 1, 5)
		dp[h][x] = -inf;
		
	if(p != -1)	
	For(i, 0, c[x].sz) {
		auto s = c[x][i];
		if(s.ff != p) {
			For(hh, 0, 5) {
				buff[hh] = dp[hh][s.ff];
				dp[hh][s.ff] = hh == 0 ? 0 : -inf;
			}
		//	banned = 1ll << (i - (c[x][0].ff == p));
		//	banned = i - (c[x][0].ff == p);
			banned_edge = {x, s.ff};
			dpsub -= dp[0][s.ff];
			lld score = brut(x, p, dpsub);
	//		cout << x << " calculated " << score << endl;
		//	lld score = 0;
			banned_edge = {-1, -1};
			For(h, 1, 5)	
				dp[h][x] = fmax(dp[h][x], buff[h - 1] + s.dd + score);
			For(hh, 0, 5)
				dp[hh][s.ff] = buff[hh];
			dpsub += dp[0][s.ff];
		}
	}
	
	//cout << x << endl;		
	/*cout << x << " ";
	For(i, 0, 5)
		cout << dp[i][x] << " ";
	puts("");*/
}

int32_t main(void){
	int n;
	scanf("%d", &n);
	if(n == 1) {
		puts("0");
		return 0;
	}
	For(i, 0, n - 1) {
		int g, h, k;
		scanf("%d%d%d", &g, &h, &k);
		c[g].pb({h, k});
		c[h].pb({g, k});
	}
	int mx = 0, cand = 1;
	For(i, 1, n + 1)
		if(mx < c[i].sz)
			mx = c[i].sz, cand = i;
	//cout << mx << endl;
	dfbuild(cand);
	printf("%lld\n", dp[0][cand]);
}