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
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
#include <cstddef>

using namespace std;

#define smol LONG_MIN
#define ll long long int

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    ll n;
    cin >> n;

    vector<pair<ll, ll>>* connections = new vector<pair<ll, ll>>[n]();

    ll a, b, cost;
    for (ll i = 0; i < n - 1; i++)
    {
        cin >> a >> b >> cost;
        a--;
        b--;

        connections[a].push_back({ b, cost });
        connections[b].push_back({ a, cost });
    }

    ll* parents = new ll[n]();
    ll* childrenNotChecked = new ll[n]();
    parents[0] = -1;

    queue<ll> toCheck;

    for (auto it : connections[0])
    {
        toCheck.push(it.first);
        parents[it.first] = 0;
    }

    while (!toCheck.empty())
    {
        for (auto it : connections[toCheck.front()])
        {
            if (it.first != parents[toCheck.front()])
            {
                toCheck.push(it.first);
                parents[it.first] = toCheck.front();
            }
            
        }
        toCheck.pop();
    }

    pair<ll, bool>** bests = new pair<ll, bool>*[n]();

    for (ll i = 0; i < n; i++)
    {
        if (connections[i].size() == 1 && parents[i] != -1)
            toCheck.push(i);

        childrenNotChecked[i] = connections[i].size() - 1;

        bests[i] = new pair<ll, bool>[4]();

        bests[i][0].second = true;
    }
    childrenNotChecked[0] = connections[0].size();

    while (!toCheck.empty())
    {
        ll checked = toCheck.front();
        toCheck.pop();

        ll freeSum = 0;

        ll reach = (connections[checked].size() - 1 / 2) + 2;
        ll** highestSum = new ll* [reach * 2 + 1](); //as first index inxreses, more free 1s
        ll** newHighestSum = new ll* [reach * 2 + 1]();

        for (ll i = 0; i < reach * 2 + 1; i++)
        {
            highestSum[i] = new ll[2]();
            newHighestSum[i] = new ll[2]();

            highestSum[i][0] = smol;
            highestSum[i][1] = smol;
            newHighestSum[i][0] = smol;
            newHighestSum[i][1] = smol;
        }

        highestSum[reach][0] = 0;

        for (auto it : connections[checked])
        {
            if (it.first != parents[checked])
            {
                ll zero = bests[it.first][0].first;
                
                if (bests[it.first][3].second)
                    zero = max(zero, bests[it.first][3].first + it.second);

                ll one = bests[it.first][0].first + it.second; 
                ll two = bests[it.first][1].first + it.second; 
                ll three = bests[it.first][2].first + it.second; 

                for (ll i = 0; i < reach * 2 + 1; i++)
                {
                    for (ll j = 0; j <= 1; j++)
                    {
                        newHighestSum[i][j] = smol;

                        if (i + 1 != reach * 2 + 1 && highestSum[i + 1][j] != smol && bests[it.first][2].second)
                            newHighestSum[i][j] = max(newHighestSum[i][j], highestSum[i + 1][j] + three);

                        if (i - 1 != -1 && highestSum[i - 1][j] != smol)
                            newHighestSum[i][j] = max(newHighestSum[i][j], highestSum[i - 1][j] + one);

                        if (highestSum[i][j] != smol)
                            newHighestSum[i][j] = max(newHighestSum[i][j], highestSum[i][j] + zero);

                        if (highestSum[i][!j] != smol && bests[it.first][1].second)
                            newHighestSum[i][j] = max(newHighestSum[i][j], highestSum[i][!j] + two);

                    }
                }

                for (ll i = 0; i < reach * 2 + 1; i++)
                {
                    for (ll j = 0; j <= 1; j++)
                    {
                        highestSum[i][j] = newHighestSum[i][j];
                    }
                }
            }
        }

        if (newHighestSum[reach][0] != smol)
        {
            bests[checked][0].first = newHighestSum[reach][0];
            bests[checked][0].second = true;
        }

        if (newHighestSum[reach + 1][0] != smol)
        {
            bests[checked][1].first = newHighestSum[reach + 1][0];
            bests[checked][1].second = true;
        }

        if (newHighestSum[reach - 1][0] != smol)
        {
            bests[checked][3].first = newHighestSum[reach - 1][0];
            bests[checked][3].second = true;
        }

        if (newHighestSum[reach][1] != smol)
        {
            bests[checked][2].first = newHighestSum[reach][1];
            bests[checked][2].second = true;
        }

        childrenNotChecked[parents[checked]]--;

        if (!childrenNotChecked[parents[checked]])
            toCheck.push(parents[checked]);
    }

    /*for (ll i = 0; i < n; i++)
    {
        for (ll j = 0; j < 4; j++)
        {

            if (!bests[i][j].second)
                cout << "- ";
            else
                cout << bests[i][j].first << ' ';
        }
        cout << '\n';
    }*/
    cout << bests[0][0].first << '\n';


}