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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const long long IMPOS = -1e16;

struct edge {
    int to;
    int weight;
};

int n,u,v,z;
vector<edge> graph[200001];
bool visited[200001];

struct tree_node {
    edge parent;
    vector<edge> children;
};

struct opt_struct {
    long long first, second, third;
    int f_info, s_info, t_info;
    void clear() {
        first = -1e16;
        f_info = -1;
        second = -1e16;
        s_info = -1;
        third = -1e16;
        t_info = -1;
    }
    void update(long long val, int info) {
        if(val > first) {
            third = second;
            t_info = s_info;
            second = first;
            s_info = f_info;
            first = val;
            f_info = info;
        } else if(val > second) {
            third = second;
            t_info = s_info;
            second = val;
            s_info = info;
        } else if(val > third) {
            third = val;
            t_info = info;
        }
    } 
    void remove(long long info) {
        if(info == f_info) {
            first = second;
            f_info = s_info;
            second = third;
            s_info = t_info;
        } else if(info == s_info) {
            second = third;
            s_info = t_info;
        }
    }
};

tree_node tree[200001];
long long opt[200001][4];

void make_tree() {
    queue<int> q;
    visited[1] = true;
    q.push(1);
    tree[1].parent = {-1,-1};
    while(!q.empty()) {
        int curr = q.front();
        q.pop();
        for(edge e : graph[curr]) {
            if(!visited[e.to]) {
                visited[e.to] = true;
                q.push(e.to);
                tree[curr].children.push_back(e);
                tree[e.to].parent = {curr, e.weight};
            }
        }
    }
}

opt_struct best[5];
pair< pair<int,int>, long long > best_new_path(int restricted) {
    opt_struct new_best[5];
    for(int i=0; i<5; i++) {
        new_best[i] = best[i];
        new_best[i].remove(restricted);
    }

    long long pos[5];
    pair<int,int> children_pos[5];
    pair<int,int> children_in_new_path;

    pos[1] = new_best[4].first == IMPOS ? IMPOS : new_best[4].first;
    children_pos[1] = {new_best[4].f_info, -1};

    if(new_best[3].first == IMPOS || new_best[1].first == IMPOS) {
        pos[2] = IMPOS;
    } else if(new_best[3].f_info != new_best[1].f_info) {
        pos[2] = new_best[3].first + new_best[1].first;
        children_pos[2] = {new_best[3].f_info, new_best[1].f_info};
    } else if(new_best[1].second != IMPOS) {
        pos[2] = new_best[3].first + new_best[1].second;
        children_pos[2] = {new_best[3].f_info, new_best[1].s_info};
    } else {
        pos[2] = IMPOS;
    }

    if(new_best[2].second == IMPOS) {
        pos[3] = IMPOS;
    } else {
        pos[3] = new_best[2].first + new_best[2].second;
        children_pos[3] = {new_best[2].f_info, new_best[2].s_info};
    }

    if(new_best[1].first == IMPOS || new_best[3].first == IMPOS) {
        pos[4] = IMPOS;
    } else if(new_best[1].f_info != new_best[3].f_info) {
        pos[4] = new_best[1].first + new_best[3].first;
        children_pos[4] = {new_best[1].f_info, new_best[3].f_info};
    } else if(new_best[3].second != IMPOS) {
        pos[4] = new_best[1].first + new_best[3].second;
        children_pos[4] = {new_best[1].f_info, new_best[3].s_info};
    } else {
        pos[4] = IMPOS;
    }

    long long best_new_length = max((long long)0, max(pos[1], max(pos[2], max(pos[3],pos[4]))));
    if(best_new_length == 0) children_in_new_path = {-1,-1};
    if(best_new_length == pos[1]) children_in_new_path = children_pos[1];
    if(best_new_length == pos[2]) children_in_new_path = children_pos[2];
    if(best_new_length == pos[3]) children_in_new_path = children_pos[3];
    if(best_new_length == pos[4]) children_in_new_path = children_pos[4];
    return {children_in_new_path, best_new_length};
}

void post_order(int v) {
    long long opt_temp = 0;
    opt[v][0] = 0;
    for(int i=1; i<=3; i++) opt[v][i] = IMPOS;
    if(!tree[v].children.empty()) opt[v][1] = 0;
    for(edge e : tree[v].children) {
        post_order(e.to);
    }

    for(int i=1; i<=4; i++) best[i].clear();
    for(edge e : tree[v].children) {
        int child = e.to;
        opt_temp += opt[child][0];
        for(int i=1; i<=4; i++) {
            if(opt[child][i-1] != IMPOS) {
                best[i].update(e.weight+opt[child][i-1]-opt[child][0], child);
            }
        }
    }
    auto best_overall_path = best_new_path(-1);
    opt[v][0] = opt_temp + best_overall_path.second;

    opt_struct new_best[5];
    for(int i=1; i<=4; i++) new_best[i].clear();
    for(edge e : tree[v].children) {
        int child = e.to;
        for(int i=1; i<=4; i++) {
            long long best_pos = e.weight;
            if(opt[child][i-1] == IMPOS) break;
            best_pos += opt[child][i-1]-opt[child][0];
            auto new_path = best_new_path(child);
            best_pos -= best_overall_path.second;
            best_pos += new_path.second;
            new_best[i].update(best_pos, child);
        }
    }

    for(int i=1; i<=3; i++) {
        opt[v][i] = best[i].first == IMPOS ? IMPOS : opt[v][0] + new_best[i].first;
    }

    // opt[v][0] = skończone drzewo
    // opt[v][1] = 1 krawędź od dołu
    // opt[v][2] = 2 krawędzie od dołu
    // opt[v][3] = 3 krawędzie od dołu
}

int main() {
    cin >> n;
    for(int i=0; i<n; i++) {
        cin >> u >> v >> z;
        graph[u].push_back({v,z});
        graph[v].push_back({u,z});
    }
    for(int i=1; i<=n; i++) {
        visited[i] = false;
    }
    make_tree();
    post_order(1);
    // for(int i=1; i<=n; i++) {
    //     cout << i << ": " << opt[i][0] << " " << opt[i][1] << " " << opt[i][2] << " " << opt[i][3] << endl;
    // }
    cout << opt[1][0] << endl;
}