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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <bits/stdc++.h>

using namespace std;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return {i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (c it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(x...) " [" #x ": " << (x) << "] "

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
template <typename A, typename B>
using unordered_map2 = __gnu_pbds::gp_hash_table<A, B>;
using namespace __gnu_pbds;
template <typename T> using ordered_set =
  __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag,
                   __gnu_pbds::tree_order_statistics_node_update>;
// ordered_set<int> s; s.insert(1); s.insert(2);
// s.order_of_key(1);    // Out: 0.
// *s.find_by_order(1);  // Out: 2.

using ld = long double;
using ll = long long;

constexpr int mod = 1000 * 1000 * 1000 + 7;
constexpr int odw2 = (mod + 1) / 2;

void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; }
int Odejmij(int a, int b) { OdejmijOd(a, b); return a; }
void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; }
int Dodaj(int a, int b) { DodajDo(a, b); return a; }
int Mnoz(int a, int b) { return (ll) a * b % mod; }
void MnozDo(int& a, int b) { a = Mnoz(a, b); }
int Pot(int a, ll b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; }
int Odw(int a) { return Pot(a, mod - 2); }
void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); }
int Podziel(int a, int b) { return Mnoz(a, Odw(b)); }
int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; }

template <typename T> T Maxi(T& a, T b) { return a = max(a, b); }
template <typename T> T Mini(T& a, T b) { return a = min(a, b); }

constexpr int nax = 200 * 1000 + 105;
constexpr ll infty = std::numeric_limits<ll>::max() / 3;

using Czworka = array<ll, 4>;

debug& operator<<(debug& deb, const Czworka& c) {
  return deb << "(" << c[0] << ", " << c[1] << ", " << c[2] << ", " << c[3] << ")";
}

constexpr int ZERO = 1 << 0;
constexpr int JEDEN = 1 << 1;
constexpr int DWA = 1 << 2;
constexpr int TRZY = 1 << 3;

class Polaczacz {
 public:
  Polaczacz(const vector<Czworka>& czworki) : czworki_(czworki) {
    m = (int) czworki.size();
    wybrane_.resize(m);
    wynik_[0] = wynik_[1] = wynik_[2] = wynik_[3] = -infty;
    koszt = 0;
    ile[0] = ile[1] = ile[2] = ile[3] = 0;
    for (int i = 0; i < m; i++) {
      const Czworka& czworka = czworki[i];
      int naj = 0;
      for (int j = 1; j < 4; j++) {
        if (czworka[naj] < czworka[j]) {
          naj = j;
        }
      }
      assert(czworka[naj] > -infty);
      koszt += czworka[naj];
      wybrane_[i] = naj;

      const int a = naj;
      ile[a]++;
      for (int b = 0; b < 4; b++) {
        if (a != b and czworka[b] != -infty) {
          kraw[a][b].emplace(czworka[b] - czworka[a], i);
          debug() << imie(kraw[a][b]) imie(czworka[b]) imie(czworka[a]) imie(-infty);
        }
      }
    }
  }

  Czworka Run() {
    while (ile[1] > 0) {
      ProwadzOk(JEDEN, ZERO | DWA);
    }
    while (ile[3] > 0) {
      ProwadzOk(TRZY, ZERO | DWA);
    }
    assert(ile[1] == 0);
    assert(ile[3] == 0);
    while (true) {
      assert(ile[1] == ile[3]);
      ZapiszRozneParzystosci();
      if (Prowadz(ZERO | DWA, JEDEN)) {
        ZapiszRozneParzystosci();
        ProwadzOk(JEDEN, ZERO | DWA);
      }
      if (Prowadz(ZERO | DWA, TRZY)) {
        ZapiszRozneParzystosci();
        ProwadzOk(TRZY, ZERO | DWA);
      }
      if (!Prowadz(ZERO | DWA, JEDEN) or
          !Prowadz(ZERO | DWA, TRZY)) {
        break;
      }
    }
    return wynik_;
  }

 private:
  void ProwadzOk(int source, int destination) {
    const bool ok = Prowadz(source, destination);
    assert(ok);
  }

  bool Prowadz(int source, int destination) {
    debug() << imie(source) imie(destination) imie(ile[0]) imie(ile[1]) imie(ile[2]) imie(ile[3]);
    ll mac[4][4];
    ll orig[4][4];
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        auto& k = kraw[i][j];
        if (k.empty()) {
          orig[i][j] = mac[i][j] = -infty;
        } else {
          orig[i][j] = mac[i][j] = k.rbegin()->first;
          debug() << imie(k);
          assert(orig[i][j] > -infty);
        }
        debug() << imie(i) imie(j) << orig[i][j];
      }
    }
    for (int c = 0; c < 4; c++) {
      for (int a = 0; a < 4; a++) {
        if (mac[a][c] == -infty) continue;
        for (int b = 0; b < 4; b++) {
          if (mac[c][b] == -infty) continue;
          Maxi(mac[a][b], mac[a][c] + mac[c][b]);
        }
      }
    }
    int s = -1, d = -1;
    ll naj = -infty;
    for (int i = 0; i < 4; i++) {
      if (source & (1 << i)) {
        for (int j = 0; j < 4; j++) {
          if (destination & (1 << j)) {
            if (naj < mac[i][j]) {
              naj = mac[i][j];
              s = i;
              d = j;
            }
          }
        }
      }
    }
    debug() << imie(s) imie(d) imie(naj);
    assert(naj >= -infty);
    if (naj == -infty) {
      return false;
    }
    int inne[2] = {-1, -1};
    int nast = 0;
    for (int i = 0; i < 4; i++) {
      if (i != s and i != d) {
        inne[nast++] = i;
      }
    }
    if (orig[s][d] == naj) {
      WybierzKrawedz(s, d);
    } else if (orig[s][inne[0]] != -infty and orig[inne[0]][d] != -infty and
               orig[s][inne[0]] + orig[inne[0]][d] == naj) {
      WybierzKrawedz(s, inne[0]);
      WybierzKrawedz(inne[0], d);
    } else if (orig[s][inne[1]] != -infty and orig[inne[1]][d] != -infty and
               orig[s][inne[1]] + orig[inne[1]][d] == naj) {
      WybierzKrawedz(s, inne[1]);
      WybierzKrawedz(inne[1], d);
    } else if (orig[s][inne[0]] != -infty and orig[inne[0]][inne[1]] != -infty and orig[inne[1]][d] != -infty and
               orig[s][inne[0]] + orig[inne[0]][inne[1]] + orig[inne[1]][d] == naj) {
      WybierzKrawedz(s, inne[0]);
      WybierzKrawedz(inne[0], inne[1]);
      WybierzKrawedz(inne[1], d);
    } else if (orig[s][inne[1]] != -infty and orig[inne[1]][inne[0]] != -infty and orig[inne[0]][d] != -infty and
               orig[s][inne[1]] + orig[inne[1]][inne[0]] + orig[inne[0]][d] == naj) {
      WybierzKrawedz(s, inne[1]);
      WybierzKrawedz(inne[1], inne[0]);
      WybierzKrawedz(inne[0], d);
    } else {
      assert(false);
    }
    return true;
  }

  void WybierzKrawedz(int a, int b) {
    assert(ile[a] > 0);
    ile[a]--;
    ile[b]++;
    auto& k = kraw[a][b];
    assert(!k.empty());
    const int i = k.rbegin()->second;
    assert(wybrane_[i] == a);
    wybrane_[i] = b;
    for (int j = 0; j < 4; j++) {
      if (czworki_[i][j] == -infty) continue;
      if (j != a) {
        kraw[a][j].erase(make_pair(czworki_[i][j] - czworki_[i][a], i));
      }
      if (j != b) {
        kraw[b][j].emplace(czworki_[i][j] - czworki_[i][b], i);
        debug() << imie(kraw[b][j]);
      }
    }
    koszt += czworki_[i][b] - czworki_[i][a];
  }

  void ZapiszRozneParzystosci() {
    Zapisz();
    if (ile[0] > 0 and Prowadz(ZERO, DWA)) {
      Zapisz();
      ProwadzOk(DWA, ZERO);
    }
    if (ile[2] > 0) {
      ProwadzOk(DWA, ZERO);
      Zapisz();
      ProwadzOk(ZERO, DWA);
    }
  }

  void Zapisz() {
    const int bilans = ile[1] - ile[3];
    const int parz = ile[2] % 2;
    if (bilans == 0 and parz == 0) {
      Maxi(wynik_[0], koszt);
    } else if (bilans == 1 and parz == 0) {
      Maxi(wynik_[1], koszt);
    } else if (bilans == 0 and parz == 1) {
      Maxi(wynik_[2], koszt);
    } else if (bilans == -1 and parz == 0) {
      Maxi(wynik_[3], koszt);
    }
  }

  int m;
  vector<int> wybrane_;
  const vector<Czworka>& czworki_;

  ll koszt;
  int ile[4];
  set<pair<ll, int>> kraw[4][4];

  Czworka wynik_;
};

Czworka Polacz(const vector<Czworka>& czworki) {
  #ifdef LOCAL
  debug() << "Polacz(" imie(czworki) ")";
  Czworka c = Polaczacz(czworki).Run();
  debug() << "`---> wynik = " << c;
  for (int i = 0; i < 4; i++) {
    assert(c[i] == -infty or c[i] > -infty/10);
  }
  return c;
  #else
  return Polaczacz(czworki).Run();
  #endif
}

Czworka Rozszerz(const Czworka& czworka, int c) {
  Czworka res;

  res[0] = czworka[0];
  if (czworka[3] != -infty) Maxi(res[0], czworka[3] + c);

  if (czworka[0] == -infty) res[1] = -infty;
  else res[1] = czworka[0] + c;

  if (czworka[1] == -infty) res[2] = -infty;
  else res[2] = czworka[1] + c;

  if (czworka[2] == -infty) res[3] = -infty;
  else res[3] = czworka[2] + c;

  return res;
}

int n;
vector<pair<int, int>> graf[nax];

Czworka Dfs(int w, int o) {
  vector<Czworka> czworki;
  for (const auto& [v, c] : graf[w]) {
    if (v == o) continue;
    czworki.push_back(Rozszerz(Dfs(v, w), c));
  }
  return Polacz(czworki);
}

int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    graf[a].emplace_back(b, c);
    graf[b].emplace_back(a, c);
  }
  cout << Dfs(1, -1)[0] << endl;
  return 0;
}