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
#include <bits/stdc++.h>

using namespace std;

int n;

constexpr int ITER = 2;
constexpr int LEN = 4;
constexpr int64_t neginf = numeric_limits<int64_t>::min();

struct Edge { int v, z; };

vector<vector<Edge>> nb;

struct Result { int64_t z[LEN]; };

struct Range
{
    int start = 0;
    vector<int64_t> v;
    int end() const { return start + (int) v.size(); }
    bool emtpy() const { return v.empty(); }
    Range() {}
    Range(int start, int finish) : start(start), v(finish - start) {}
    Range(int start, const std::initializer_list<int64_t>& l) : start(start), v(l) {
        while (!v.empty() && v.back() == neginf) v.pop_back();
        while (!v.empty() && v[0] == neginf) { start++; v.erase(v.begin()); }
    }
    int64_t& At(int i) {
        assert(i >= start && i < end());
        return v[i - start];
    }
    int64_t operator[](int i) const {
        if (i < start)
            return neginf;
        i -= start;
        if (i >= (int) v.size())
            return neginf;
        return v[i];
    }
};

struct Stat { Range s[2]; };

Range Max(const Range& a, const Range& b)
{
    Range c(min(a.start, b.start), max(a.end(), b.end()));
    for (int i = c.start; i < c.end(); ++i)
        c.At(i) = max(a[i], b[i]);
    return c;
}

int64_t sum(int64_t a, int64_t b)
{
    if (a == neginf || b == neginf)
        return neginf;
    return a + b;
}

Range Merge(const Range& a, const Range& b)
{
    if (a.emtpy() || b.emtpy())
        return Range();
    Range c(a.start + b.start, a.end() + b.end() - 1);
    int i = a.start;
    int j = b.start;
    c.At(c.start) = a[i] + b[j];
    for (int k = c.start + 1; k < c.end(); ++k) {
        c.At(k) = neginf;
        int pi = i, pj = j;
        if (i + 1 < a.end())
            i++;
        else
            j++;
        for (int iter = 0, ii = pi, jj = pj; iter < ITER; iter++) {
            ++ii;
            int64_t x = sum(a[ii], b[jj]);
            if (x > c[k]) {
                i = ii;
                j = jj;
                c.At(k) = x;
            }
            --jj;
        }
        for (int iter = 0, ii = pi, jj = pj; iter < ITER; iter++) {
            ++jj;
            int64_t x = sum(a[ii], b[jj]);
            if (x > c[k]) {
                i = ii;
                j = jj;
                c.At(k) = x;
            }
            --ii;
        }
    }
    return c;
}

Stat FindStat(const vector<Result>& ch, int l, int r)
{
    if (l + 1 == r)
        return Stat{{
            Range(-1, {ch[l].z[0], ch[l].z[3], ch[l].z[2]}),
            Range(0, {ch[l].z[1]})
        }};
    Stat res;
    int m = (l + r) / 2;
    Stat a = FindStat(ch, l, m);
    Stat b = FindStat(ch, m, r);
    for (int i = 0; i < 2; ++i)
        res.s[i] = Max(Merge(a.s[0], b.s[i]), Merge(a.s[1], b.s[i ^ 1]));
    return res;
}

Result Calc(int u, int p = -1)
{
    auto& l = nb[u];
    for (int i = 0; i < (int) l.size(); ++i)
        if (l[i].v == p) {
            swap(l[i], l.back());
            l.pop_back();
            break;
        }
    int m = l.size();

    Result res;
    if (m == 0) {
        for (int i = 0; i < LEN; ++i)
            res.z[i] = i ? neginf : 0;
        return res;
    }

    vector<Result> ch(m);
    for (int i = 0; i < m; ++i)
        ch[i] = Calc(l[i].v, u);

    for (int i = 0; i < m; ++i) {
        int64_t base_i = ch[i].z[0];
        for (int k = 0; k < LEN; ++k)
            if (ch[i].z[k] != neginf)
                ch[i].z[k] += l[i].z;
        ch[i].z[3] = max(ch[i].z[3], base_i);
    }

    auto stat = FindStat(ch, 0, m);
    res.z[0] = stat.s[0][0];
    res.z[1] = stat.s[0][-1];
    res.z[2] = stat.s[1][0];
    res.z[3] = stat.s[0][1];

    return res;
}

int main()
{
    scanf("%d", &n);
    nb.resize(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v, z;
        scanf("%d%d%d", &u, &v, &z);
        u--; v--;
        nb[u].push_back(Edge{v, z});
        nb[v].push_back(Edge{u, z});
    }
    printf("%ld\n", Calc(0).z[0]);
    return 0;
}