This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// fest
#include <bits/stdc++.h>	
#define pb push_back
#define F first
#define S second
#define y1 dasdasfasfas
#define x1 wqdadfasfasfas
#define All(c) c.begin(), c.end()
#define SZ(A) (int((A).size()))
#define umap unordered_map
#define FILENAME ""
#define __ fflush(stdout)
typedef long long ll;
typedef long double ld;    
using namespace std;
void FREOPEN() {
	#ifdef COMP
		freopen(".in", "r", stdin);
		freopen("1.out", "w", stdout);
	#endif
}                           
inline double Time() {return (clock() * 1.0) / CLOCKS_PER_SEC; }             
const int N = 200500, inf = 1e9 * 2, MOD = (int)1e9 + 7;
char CH[N];
const ll INF = 1e18;
const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};
const int dy[] = {0, 0, 1, -1, -1, 1, 1, -1};
int dp[N][2];
vector<pair<int, int> > g[N];
pair<int, int> opt[N];
int ans;
void dfs(int v, int pr, int last) {
	dp[v][0] = 0;
	dp[v][1] = -inf;
	for (auto i : g[v]) {
		int u = i.F;
		int c = i.S;
		if (u == pr) continue;
		dfs(u, v, c);
		dp[v][0] += max(dp[u][0], dp[u][1]);
	}
	for (auto i : g[v]) {
		int u = i.F;
		int c = i.S;
		if (u == pr) continue;
		if (dp[v][1] < last + (dp[v][0] - max(dp[u][0], dp[u][1])) + c + dp[u][0]) {
			dp[v][1] = last + (dp[v][0] - max(dp[u][0], dp[u][1])) + c + dp[u][0];
			opt[v] = {u, c};
		}
	}
}
void calc(int v, int pr, int last, int dp0, int dp1) {
	ans = max(ans, dp[v][0] + max(dp0, dp1));
	for (auto i : g[v]) {
		int u = i.F;
		int c = i.S;
		if (u == pr) continue;
		int go0 = dp[v][0] - max(dp[u][0], dp[u][1]) + max(dp0, dp1);
		int go1 = -inf;
		if (last) go1 = go0 - max(dp0, dp1) + last + dp0 + c;
		if (opt[v].F != u) {
			go1 = max(go1, go0 - max(dp[opt[v].F][0], dp[opt[v].F][1]) + opt[v].S + c + dp[opt[v].F][0]); 
		}
		else {
			for (auto j : g[v]) {
				int u2 = j.F;
				int c2 = j.S;
				if (u2 == pr || u2 == u) continue;
				go1 = max(go1, go0 - max(dp[u2][0], dp[u2][1]) + c2 + c + dp[u2][0]);
			}
		}
		calc(u, v, c, go0, go1);
	}
}
int main() {
  
	FREOPEN();
	int n;
	cin >> n;
	for (int i = 2; i <= n; i++) {
		int x, y, c;
		scanf("%d %d %d", &x, &y, &c);
		g[x].pb({y, c});
		g[y].pb({x, c});
	}
	dfs(1, 0, 0);
	calc(1, 0, 0, 0, -inf);
	cout << ans;
	return 0;	
}
Compilation message (stderr)
beads.cpp: In function 'int main()':
beads.cpp:98:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &x, &y, &c);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |