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
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define e1 first
#define e2 second
#define pb push_back
#define OUT(x) {cout << x << "\n"; exit(0); }
#define TCOUT(x) {cout << x << "\n"; return; }
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
#define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }
#define sz(x) int(x.size())
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
/*#include <atcoder/modint>
using namespace atcoder;
using mint = modint998244353;
vector <mint> fac, inv;
mint binom(int n, int k) {
	if (n < k || n < 0) return 0;
	return fac[n] * inv[k] * inv[n-k];
}

void prep(int N) {
	fac.resize(N+1, 1); inv.resize(N+1, 1);
	for (int i=1; i<=N; ++i) fac[i] = fac[i-1] * i;
	inv[N] = fac[N].inv();
	for (int i=N-1; i>0; --i) inv[i] = inv[i+1] * (i + 1);
}*/

mt19937_64 rng(time(0));
int random(int l, int r) {
	return uniform_int_distribution<int>(l, r)(rng);
}
#ifdef DEBUG
template<class T> int size(T &&x) {
	return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
	out << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		out << *it << (it == prev(x.end()) ? "" : ", ");
	return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
	cerr << x << ";  ";
	dump(args...);
}
#endif
#ifdef DEBUG
  struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (strcmp(#x, "") ? #x ":  " : ""), dump(x), Nl(), cerr << ""
#else
# define debug(...) 0 && cerr
#endif


const int maxn = 200100;
const ll INF = (LLONG_MAX / 8LL);
const int C = 1000;

ll brut(vector <vector <pii>> graf) {
	return 0LL;
}

vector <array<ll, 4> > dp;
inline void maxuj(ll &x, ll val) { x = max(x, val); }

ll threes_over_ones[2 * C + 1][2];
ll old[2 * C + 1][2];

void dfs(vector <vector <pii> > &graf, int x, int par = -1, int toparent = -1) {
	vector <pii> ch;
	for (auto u : graf[x]) {
		if (u.first != par) {
			ch.pb(u);
			dfs(graf, u.first, x, u.second);
		}
	}
	
	if (ch.empty()) { //totally no sons
		assert(x != 0);
		dp[x][1] = toparent;
		dp[x][0] = 0;
	}
	else {
		FOR(i, 0, 2*C) threes_over_ones[i][0] = threes_over_ones[i][1] = -INF;
		threes_over_ones[0 + C][0] = 0;
		// THE MOST IMPORTANT LINE IN THE ENTIRE SOLUTION
		random_shuffle(all(ch));
		// WE NEED TO PROCESS SONS IN ARBITRARY ORDER
		for (auto &[son, _] : ch) {
			int range = 2 * C;
			ll no_change = dp[son][0];
			ll one = dp[son][1];
			ll two = dp[son][2];
			ll three = dp[son][3];
			memcpy(old, threes_over_ones, (4 * C + 2) * sizeof(ll));
			for (int i = 0; i <= range; ++i) {
				maxuj(threes_over_ones[i][0], old[i][1] + two);
				maxuj(threes_over_ones[i][1], old[i][0] + two);
				
				maxuj(threes_over_ones[i][0], old[i][0] + no_change);
				maxuj(threes_over_ones[i][1], old[i][1] + no_change);
				
				if (i > 0) {
					maxuj(threes_over_ones[i][0], old[i-1][0] + three);
					maxuj(threes_over_ones[i][1], old[i-1][1] + three);
				}
				
				if (i < range) {
					maxuj(threes_over_ones[i][0], old[i+1][0] + one);
					maxuj(threes_over_ones[i][1], old[i+1][1] + one);
				}
			}
		}
		
		vector<ll> temp(4);
		temp[0] = max(0LL, threes_over_ones[C][0]);
		temp[1] = threes_over_ones[C-1][0];
		temp[2] = threes_over_ones[C][1];
		temp[3] = threes_over_ones[C+1][0];
		
		if (x > 0) { //lift to parent
			dp[x][0] = max(temp[0], temp[3] + toparent);
			dp[x][1] = temp[0] + toparent;
			dp[x][2] = temp[1] + toparent;
			dp[x][3] = temp[2] + toparent;
		}
		else {
			rep(i, 0, 4) dp[x][i] = temp[i];
		}
	}
}

ll wzo(vector <vector <pii>> graf) {
	int n = sz(graf);
	dp.clear();
	dp.resize(n, {-INF, -INF, -INF, -INF});
	dfs(graf, 0, 0);
	return dp[0][0];
}

bool TEST = 0;
const int MAX = 1e9;

int main() {
	srand(444444132);
	boost;
	int n; vector <vector <pii> > graf;
	
	if (!TEST) {
		cin >> n;
		graf.resize(n);
		rep(i, 1, n) {
			int a, b, c;
			cin >> a >> b >> c;
			--a; --b;
			graf[a].pb({b, c});
			graf[b].pb({a, c});
		}
		
		cout << wzo(graf) << "\n";
	}
	else {
		//TEST GENERATION
		int tests = 1;
		while (tests++) {
			cout << "OK: " << tests << endl;
			n = 200000;
			graf.clear(); graf.shrink_to_fit();
			graf.resize(n);
			rep(i, 1, n) {
				int a = i, b = random(0, i-1), c = random(-MAX, MAX);
				graf[a].pb({b, c});
				graf[b].pb({a, c});
			}
			
			//END TEST GENERATION
			srand(2137);
			auto model = wzo(graf);
			srand(666);
			auto brut = wzo(graf);
			assert(model == brut);
		}
	}

}