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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_N	200000
#define K	2000
#define NEG_INF	-1e15

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

struct edge {
	int a, b, w;
};

struct h_edge {
	int u, w;
};

struct edge edges[MAX_N];
struct h_edge data[MAX_N * 2];

int deg[MAX_N + 1];
struct h_edge *ch[MAX_N + 1];

struct dp_res {
	double x0, x1, x2, x3;
};
struct dp_res dp[MAX_N + 1];

struct sub {
	double a, b, c, d;
};
struct sub sub[MAX_N];

double sub_res[2][2*K+1][2];

void build_tree(int n)
{
	int i;
	int a, b, w;

	for (i = 0; i < n - 1; i++) {
		scanf("%d%d%d", &a, &b, &w);
		edges[i].a = a;
		edges[i].b = b;
		edges[i].w = w;
		deg[a]++;
		deg[b]++;
	}

	ch[1] = data;
	for (i = 1; i < n; i++)
		ch[i+1] = ch[i] + deg[i];
	memset(deg, 0, (n + 1) * sizeof(deg[0]));

	for (i = 0; i < n - 1; i++) {
		a = edges[i].a;
		b = edges[i].b;
		w = edges[i].w;
		ch[a][deg[a]].u = b;
		ch[a][deg[a]].w = w;
		ch[b][deg[b]].u = a;
		ch[b][deg[b]].w = w;
		deg[a]++;
		deg[b]++;
	}
}

void shuffle(int s)
{
	struct sub tmp;
	int r;
	int i;

	for (i = 1; i < s; i++) {
		r = rand() % i;
		tmp = sub[r];
		sub[r] = sub[i];
		sub[i] = tmp;
	}
}

int run_sub(int s)
{
	int i, j, k;
	int c = 0;
	double res;

	for (j = 0; j < 2 * K + 1; j++) {
		for (k = 0; k < 2; k++)
			sub_res[0][j][k] = NEG_INF;
	}
	sub_res[0][K][0] = 0;

	for (i = 0; i < s; i++) {
		for (k = 0; k < 2; k++) {
			res = sub[i].b + sub_res[c][1][k];
			res = max(res, sub[i].c + sub_res[c][0][k]);
			res = max(res, sub[i].d + sub_res[c][0][!k]);
			sub_res[!c][0][k] = res;
			for (j = 1; j < 2 * K; j++) {
				res = sub[i].a + sub_res[c][j-1][k];
				res = max(res, sub[i].b + sub_res[c][j+1][k]);
				res = max(res, sub[i].c + sub_res[c][j][k]);
				res = max(res, sub[i].d + sub_res[c][j][!k]);
				sub_res[!c][j][k] = res;
			}
			res = sub[i].a + sub_res[c][2*K-1][k];
			res = max(res, sub[i].c + sub_res[c][2*K][k]);
			res = max(res, sub[i].d + sub_res[c][2*K][!k]);
			sub_res[!c][2*K][k] = res;
		}
		c = !c;
	}
	return c;
}

void run_dp(int v, int parent)
{
	int s = 0;
	struct h_edge *h;
	int c;
	int i;

	for (i = 0; i < deg[v]; i++) {
		h = &ch[v][i];
		if (h->u == parent)
			continue;
		run_dp(h->u, v);
	}
	for (i = 0; i < deg[v]; i++) {
		h = &ch[v][i];
		if (h->u == parent)
			continue;
		sub[s].a = dp[h->u].x0 + h->w;
		sub[s].b = dp[h->u].x2 + h->w;
		sub[s].c = max(dp[h->u].x0, dp[h->u].x1 + h->w);
		sub[s].d = dp[h->u].x3 + h->w;
		s++;
	}
	shuffle(s);
	c = run_sub(s);
	dp[v].x0 = sub_res[c][K][0];
	dp[v].x1 = sub_res[c][K-1][0];
	dp[v].x2 = sub_res[c][K][1];
	dp[v].x3 = sub_res[c][K+1][0];
}

int main(void)
{
	int n;

	scanf("%d", &n);
	build_tree(n);
	run_dp(1, 1);
	printf("%lld\n", (long long)dp[1].x0);

	return 0;
}