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
// Arti1990, II UWr

#include <bits/stdc++.h>

#define forr(i, n)                  for(int i=0; i<n; i++)
#define FOREACH(iter, coll)         for(auto iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll)        for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,K,FUN)             ({auto SSS=P, PPP = P-1, KKK=(K)+1; while(PPP+1!=KKK) {SSS = (PPP+(KKK-PPP)/2); if(FUN(SSS)) KKK = SSS; else PPP = SSS;} PPP;})
#define testy()                     int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests)
#define CLEAR(tab)                  memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll)           (coll.find(el) != coll.end())
#define FOR(i, a, b)                for(int i=a; i<=b; i++)
#define FORD(i, a, b)               for(int i=a; i>=b; i--)
#define MP                          make_pair
#define PB                          push_back
#define ff                          first
#define ss                          second
#define deb(X)                      X;

#define M 1000000007
#define INF 1000000000000000007LL
#define MAX 1000
#define SR 20017

using namespace std;

int n, m, a, b, c;
long long pom[2][2][40444];

struct drzewo
{
    vector <pair<int, int>> l;
};
drzewo tab[200007];

bool comp(vector <long long> &a, vector <long long> &b)
{
    return a[1]-a[3] < b[1]-b[3];
}

void szufluj(vector <vector <long long>> &vv)
{
    /*
    sort(vv.begin(), vv.end(), comp);

    int ile = vv.size();
    for(int i=1; 2*i<ile; i+=2)
    {
        swap(vv[i], vv[ile-1-i]);
    }
*/
    random_shuffle(vv.begin(), vv.end());
}

vector <long long> ruszaj(int nr, int o)
{
    int maks = 0;
    vector <vector<long long>> vv;
    for(auto el : tab[nr].l)
        if(el.ff != o)
        {
            auto v = ruszaj(el.ff, nr);
            vector <long long> v2;
            v2.PB(max(v[0], v[3]+el.ss));
            v2.PB(v[0] + el.ss);
            v2.PB(v[1] + el.ss);
            v2.PB(v[2] + el.ss);
            vv.PB(v2);

            //cout << v2[3] << endl;

            if(v2[3] > -1000000000007ll)
                maks++;
        }

    szufluj(vv);

    maks = min(maks+2, min((int)vv.size()/2+3, MAX));

    FOR(i, -maks-2, maks+2)
        FOR(b, 0, 1)
            pom[1][b][SR+i] = -INF;
    pom[1][0][SR+0] = 0;

    forr(i, vv.size())
    {
        auto v = vv[i];
        FOR(i, -maks-2, maks+2)
            FOR(b, 0, 1)
                pom[0][b][SR+i] = pom[1][b][SR+i];
        FOR(i, -maks, maks)
            FOR(b, 0, 1)
            {
                pom[1][b][SR+i] = pom[0][b][SR+i] + v[0];
                pom[1][b][SR+i] = max(pom[1][b][SR+i], pom[0][b][SR+i+1] + v[1]);
                pom[1][b][SR+i] = max(pom[1][b][SR+i], pom[0][1-b][SR+i] + v[2]);
                pom[1][b][SR+i] = max(pom[1][b][SR+i], pom[0][b][SR+i-1] + v[3]);
            }
    }
    vector <long long> v;
    v.PB(pom[1][0][SR]);
    v.PB(pom[1][0][SR-1]);
    v.PB(pom[1][1][SR]);
    v.PB(pom[1][0][SR+1]);

//    cout << nr << " zwraca " << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << endl;

    return v;
}

int solve()
{
    if(true)
    {
        scanf("%d", &n);
        forr(i, n-1)
        {
            scanf("%d %d %d", &a, &b, &c);
            tab[a].l.PB(MP(b, c));
            tab[b].l.PB(MP(a, c));
        }
    }
    else
    {
        n = 200001;
        c = 1;
        /*FOR(i, 2, n)
        {
            a = i-1;
            b = i;
            tab[a].l.PB(MP(b, c));
            tab[b].l.PB(MP(a, c));
        };*/

        FOR(i, 2, n/4+1)
        {
            a = 1;
            b = i;
            tab[a].l.PB(MP(b, c));
            tab[b].l.PB(MP(a, c));
        }
        /* */
        for(int i=n/4+2; i<n; i+=3)
        {
            a = 1;
            b = i;
            tab[a].l.PB(MP(b, c));
            tab[b].l.PB(MP(a, c));
            a = i;
            b = i+1;
            tab[a].l.PB(MP(b, c));
            tab[b].l.PB(MP(a, c));
            a = i+1;
            b = i+2;
            tab[a].l.PB(MP(b, c));
            tab[b].l.PB(MP(a, c));
        }/* */
    }

    auto v = ruszaj(1, -1);

    printf("%lld\n", v[0]);

    return 0;
}

int main()
{
    srand(7324);
    //cout << RAND_MAX << endl;
    //forr(i, 100)
    //    testuj();

    solve();

    return 0;
}