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
#include <bits/stdc++.h>
#include <chrono>
#include <math.h>
using namespace std;
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0);
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(c) (c).begin(), (c).end()
#define sz(X) (int)((X).size())
typedef long double ld;
typedef unsigned long long ull;
typedef long long ll;
// mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());uniform_int_distribution<int> distr(0, 1e9);auto my_rand = bind(distr, gen); // my_rand() zwraca teraz liczbe z przedzialu [a, b]
#ifdef LOCAL
ostream &operator<<(ostream &out, string str) {
   for (char c : str)
      out << c;
   return out;
}
template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; }
template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) {
   auto &[a, b, c] = p;
   return out << "(" << a << ", " << b << ", " << c << ")";
}
template <class L, class R, class S, class P> ostream &operator<<(ostream &out, tuple<L, R, S, P> p) {
   auto &[a, b, c, d] = p;
   return out << "(" << a << ", " << b << ", " << c << ", " << d << ")";
}
template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
   out << '{';
   for (auto it = a.begin(); it != a.end(); it = next(it))
      out << (it != a.begin() ? ", " : "") << *it;
   return out << '}';
}
void dump() { cerr << "\n"; }
template <class T, class... Ts> void dump(T a, Ts... x) {
   cerr << a << ", ";
   dump(x...);
}
#define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define int long long

const int MAXN = 2e5 + 99;
const int MAXS = 2500;
const int ZERO = MAXS / 2;
const int inf = 1e18;

#define best(a, b) a = a > b ? a : b;
//#define best(a, b) a = max(a, b)

// void best(int &a, int b) { a = a > b ? a : b; }

typedef tuple<int, int, int, int> tpl;
typedef array<array<int, MAXS + 2>, 2> Arr;

void clear(Arr &A) { rep(i, MAXS) A[0][i] = A[1][i] = -inf; }
void is_clear(Arr &A) { rep(i, MAXS) assert(i == 0 || (A[0][i] == -inf && A[1][i] == -inf)); }

Arr get_empty() {
   Arr A;
   clear(A);
   return A;
}

Arr A = get_empty(), B = get_empty();

void combine(tpl t, Arr &B, Arr &res) {
   auto [a, b, c, d] = t;
   // is_clear(res);
   for (int i = 1; i < MAXS; i++) {
      best(res[0][i], B[0][i] + a);
      best(res[1][i], B[1][i] + a);

      best(res[1][i - 1], B[1][i] + b);
      best(res[0][i - 1], B[0][i] + b);

      best(res[1][i + 1], B[1][i] + d);
      best(res[0][i + 1], B[0][i] + d);

      best(res[1][i], B[0][i] + c);
      best(res[0][i], B[1][i] + c);

      B[0][i] = B[1][i] = -inf;
   }
   swap(res, B);
}

mt19937 rng(time(NULL));

tpl combine(vector<tpl> &T) {
   if (sz(T) == 0)
      return {0, -inf, -inf, -inf};
   if (sz(T) == 1)
      return T[0];

   shuffle(all(T), rng);
   clear(A);
   A[0][ZERO] = 0;
   for (auto t : T)
      combine(t, A, B);
   return {A[0][ZERO], A[0][ZERO - 1], A[1][ZERO], A[0][ZERO + 1]};
}

vector<pii> G[MAXN];

tpl mod(tpl t, int w) {
   auto [a, b, c, d] = t;
   return {max(a, d + w), a + w, b + w, c + w};
}

tpl dfs(int x, int p = -1) {

   vector<tpl> T;
   for (auto [y, w] : G[x])
      if (y != p) {
         auto t = dfs(y, x);
         T.push_back(mod(t, w));
      }
   auto t = combine(T);
   return t;
}

int get(int a) {
   auto [r, _, __, ___] = dfs(a);
   return r;
}

int32_t main() {
   _upgrade;
   int n = 2e5;
   cin >> n;
   rep(i, n - 1) {
      int a, b, c;
      // int a = 1, b = i + 2, c = rand();
      cin >> a >> b >> c;
      G[--a].push_back({--b, c});
      G[b].push_back({a, c});
   }
   int a = get(0);
   cout << a << endl;
}