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
#include<bits/stdc++.h>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("trapv")

#define st first
#define nd second
#define pb(x) push_back(x)
#define pp(x) pop_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)

using namespace std;

///~~~~~~~~~~~~~~~~~~~~~~~~~~

void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);

///~~~~~~~~~~~~~~~~~~~~~~~~~

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;

#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

const int N=2e5+5, mod=1e9+7;
const ll INF=1e18;
vector<ll> dp[N][2];
vector<pii> E[N];
int wei[N];
vl comb(vl &A, vl &B){
	vl ans(A.size()+B.size()-1, -INF);
	int l1=0, l2=0, r1=A.size()-1, r2=B.size()-1;
	while(l1<A.size() && A[l1]+INF/2<0)l1++;
	while(l2<B.size() && B[l2]+INF/2<0)l2++;
	while(r1>=0 && A[r1]+INF/2<0)r1--;
	while(r2>=0 && B[r2]+INF/2<0)r2--;
	/*for(int i=0; i<A.size(); i++){
		for(int j=0; j<B.size(); j++){
			cout<<A[i]+B[j]<<" ";
		}
		cout<<"\n";
	}
	cout<<"\n";*/
	/*for(int i=0; i<A.size(); i++){
		for(int j=0; j<B.size(); j++){
			ans[i+j]=max(A[i]+B[j], ans[i+j]);
		}
	}*/
	for(int i=0; i<ans.size(); i++){
		ll t=-INF;
		for(int c=0; c<2; c++){
			int l=max(l1, i-r2), r=min(r1, i-l2);
			if((l&1)!=c)l++;
			if((r&1)!=c)r--;
			if(l>r)continue;
			l=(l-c)/2;
			r=(r-c)/2;
			bool b=0;
			/*for(int j=l; j<r; j++){
				ll s=A[2*j+c+2]+B[i-2*j-2-c]-A[2*j+c]-B[i-2*j-c];
				if(s<0)b=1;
				if(b==1){
					if(s>=0){
						assert(false);
					}
				}
			}
			for(int j=l+1; j<r; j++){
				ll s=A[2*j+c+2]+B[i-2*j-2-c]-2*A[2*j+c]-2*B[i-2*j-c]+(A[2*j+c-2]+B[i-2*j+2-c]);
				assert(s<=0);
			}*/			
			//assert(l<A.size() && r<A.size() && r>=0 && l>=0 && i>=r && i>=l);
			//assert(l<=r);
			//deb(i, l, r);
			while(l<r){
				int m=(r-l)/3;
				if(A[2*(l+m)+c]+B[i-2*(l+m)-c]>A[2*(r-m)+c]+B[i-2*(r-m)-c])r-=m+1;
				else l+=m+1;
			}
			if(2*l+c<A.size() && i>=2*l+c)t=max(t, A[2*l+c]+B[i-(2*l+c)]);
		}
		/*cout<<i<<"\n";
		if(ans[i]!=t){
			for(int j=0; j<=i; j++){
				if(j<A.size() && i-j<B.size())cout<<A[j]<<" "<<B[i-j]<<" "<<A[j]+B[i-j]<<"\n";
			}
			cout<<endl;
		}*/
		if(ans[i]+INF/2<0)ans[i]=-INF;
		//assert(ans[i]==t);
		ans[i]=t;
	}
	return ans;
}
pair<vl, vl> solve(vector<int> &V){
	pair<vl, vl> akt;
	akt.st={0};
	akt.nd={-INF};
	if(V.size()==0){
		return akt;
	}
	if(V.size()==1){
		int i=0;
		int k=dp[V[i]][0].size()/2;
		pair<vl, vl> akt2=mp(vl(3, -INF), vl(3, -INF));
		akt2.st[1]=dp[V[i]][0][k];
		if(k)akt2.nd[1]=max(wei[V[i]]+dp[V[i]][0][k+1], -INF);
		if(k)akt2.st[1]=max(wei[V[i]]+dp[V[i]][0][k-1], akt2.st[1]);
		akt2.st[2]=max(wei[V[i]]+dp[V[i]][0][k], -INF);
		akt2.st[0]=max(wei[V[i]]+dp[V[i]][1][k], -INF);		
		return akt2;
	}
	vi V2, V3;
	for(int i:V){
		if(V2.size()<V3.size())V2.pb(i);
		else V3.pb(i);
	}
	akt=solve(V2);
	pair<vl, vl> akt2=solve(V3);
	vl ans[4]={comb(akt.st,  akt2.st), comb(akt.st,  akt2.nd), comb(akt.nd,  akt2.st), comb(akt.nd,  akt2.nd)};
	for(int i=0; i<ans[0].size(); i++){
		ans[0][i]=max(ans[0][i], ans[3][i]);
		ans[1][i]=max(ans[1][i], ans[2][i]);
	}
	akt=mp(ans[0], ans[1]);
	return akt;
}
void dfs(int v, int p){
	vi V;
	for(auto u:E[v]){
		if(u.st!=p){
			V.pb(u.st);
			wei[u.st]=u.nd;
			dfs(u.st, v);
		}
	}
	//cout<<v<<" "<<V.size()<<"\n";
	auto a=solve(V);
	dp[v][0]=a.st;
	dp[v][1]=a.nd;
}
int main(){
	BOOST;
	int n;
	cin>>n;
	//n=2e5;
	for(int i=1; i<n; i++){
		int u, v, w;
		cin>>u>>v>>w;
		//u=i;
		//v=min(n, i*2);
		//w=rng()-1e9;
		E[u].pb(mp(v, w));
		E[v].pb(mp(u, w));
	}
	int start=1;
	dfs(start, 0);
	/*for(int i=1; i<=n; i++){
		cout<<i<<"0 ";
		for(int j=0; j<dp[i][0].size(); j++)cout<<dp[i][0][j]<<" ";
			cout<<"\n";
		cout<<i<<"1 ";
		for(int j=0; j<dp[i][0].size(); j++)cout<<dp[i][1][j]<<" ";
		cout<<"\n";
	}*/
	//cout<<"a";
	cout<<dp[start][0][dp[start][0].size()/2]<<"\n";
}