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

//#define TEST

using namespace std;

#define FAST  ios_base::sync_with_stdio(0) ;cin.tie(0);

#define pb push_back
#define mp make_pair

#define fi(a, b) for(int i=a; i<=b; i++)
#define fj(a, b) for(int j=a; j<=b; j++)
#define fo(a, b) for(int o=a; o<=b; o++)

#define fdi(a, b) for(int i=a; i>=b; i--)
#define fdj(a, b) for(int j=a; j>=b; j--)
#define fdo(a, b) for(int o=a; o>=b; o--)

#define sz(x) (int)x.size()

#define init_v( tab, n ) fo(1, n ) tab.pb(0)

typedef long long ll;
typedef long double ld;
typedef vector <int> vi;
typedef pair <int, int> pii;
typedef vector <pii> vpii;
typedef pair <ll, ll> pll;
typedef vector <pll> vpll;
typedef vector <ll> vll;

template <typename T> ostream& operator<<( ostream& os, vector<T> v)
{
    fi(0, sz(v) - 1)   os << v[i] << " ";
    return os;
}
template <typename A, typename B> ostream& operator <<(ostream& os, pair< A, B> p)
{
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream& operator<<( ostream& os, set<T> t)
{
    for (auto z : t)
    {
        os << z << " ";
    }
    return os;
}
template <typename T1, typename T2> ostream& operator <<(ostream& os, map< T1, T2> t)
{
    cerr << endl;
    for (auto z : t)
    {
        os << "\t" << z.first << " -> " << z.second << endl;
    }
    return os;
}

#ifdef TEST
#define dbg(x) {cerr << __LINE__ << "\t" << #x << ": " << x << endl;}
#define dbg0(x, n) {cerr << __LINE__ << "\t" << #x << ": "; for (int ABC = 0; ABC < n; ABC++) cerr << x[ABC] << ' '; cerr << endl;}
#else
#define dbg(x) ;
#define dbg0(x, n) ;
#endif

void przekierowanie()
{
#ifdef TEST
    freopen( "input.txt", "r", stdin );
    freopen( "output.txt", "w", stdout );
#endif
}

#define MAXN 200008

vector <vector<pair<long, long>>> graf(  MAXN );
vector <ll> wartosci_sciezki( MAXN );
vector <ll> wartosci_tras;
vector <vector<long>> trasy; // dla kazej trasy numery sciezek
vector <vector<long>> sciezki_trasy( MAXN ); // dla kazdej sciezki lista tras przechodzacych przez sciezke

vector <long> visited_node( MAXN );

vector <long> trasy_visited;
set <vector<long>> zbior_tras;
ll max_wartosc = 0;

void szukaj_tras( long wezel, queue <long> trasa, long koszt )
{
    visited_node[ wezel ] = 1;
    auto rozmiar = trasa.size();
    if( rozmiar < 4 )
    {
        for( auto x: graf[ wezel] )
            if( visited_node[ x.first ] == 0 )
            {
                queue <long> trasa_deep = trasa;
                trasa_deep.push( x.second );
                long koszt_deep = koszt + wartosci_sciezki[ x.second ];
                szukaj_tras( x.first, trasa_deep, koszt_deep );
            }
    }
    else
    {
        if( koszt > 0)
        {
            queue <long> trasa_znaleziona = trasa;
            long l_tras = trasy.size();

            vector <long> trasa_zn;
            while( !trasa_znaleziona.empty())
            {
                auto el = trasa_znaleziona.front();
                trasa_znaleziona.pop();
                trasa_zn.pb( el );
            }
            sort( trasa_zn.begin(), trasa_zn.end());
            if( zbior_tras.find( trasa_zn )  == zbior_tras.end())
            {
                trasy_visited.pb( 0 );
                zbior_tras.insert( trasa_zn );
                trasy.pb( trasa_zn );
                wartosci_tras.pb( koszt );
                for( auto el: trasa_zn)
                    sciezki_trasy[ el ].pb( l_tras );
            }
        }
        auto pierwszy = trasa.front();
        koszt = koszt - wartosci_sciezki[ pierwszy ];
        trasa.pop();
        for( auto x: graf[ wezel] )
            if( visited_node[ x.first ] == 0 )
            {
                queue <long> trasa_deep = trasa;
                trasa_deep.push( x.second );
                long koszt_deep = koszt + wartosci_sciezki[ x.second ];
                szukaj_tras( x.first, trasa_deep, koszt_deep );
            }
    }
}



void zestawy_tras( long nr_trasy, ll wartosc )
{
    trasy_visited[ nr_trasy ] = 1;
    auto wartosc_tej_trasy = wartosci_tras[ nr_trasy ];
    if( wartosc + wartosc_tej_trasy > max_wartosc )
        max_wartosc = wartosc + wartosc_tej_trasy ;

    vector <long> trasy_kolidujace;
    for( long i = 0; i < trasy[ nr_trasy ]. size(); i ++ )
    {
        auto nr_sciezki = trasy[ nr_trasy ][ i ];
        for( long j = 0; j < sciezki_trasy[ nr_sciezki ].size(); j ++ )
        {
            auto num_tra = sciezki_trasy[ nr_sciezki ][ j ];
            if( !trasy_visited[ num_tra ])
            {
                trasy_visited[ num_tra ] = 1;
                trasy_kolidujace.pb( num_tra );
            }
        }
    }

    for( long i = 0;  i < trasy.size(); i ++ )
        if( !trasy_visited[ i ] )
            zestawy_tras( i, wartosc + wartosc_tej_trasy);

    for( auto trkol: trasy_kolidujace )
        trasy_visited[ trkol ] = 0;

         trasy_visited[ nr_trasy ] = 0;
}

int main()
{
    FAST
    przekierowanie();
    long n;
    cin >> n;

    long numer_sciezki = 0;
    auto x = n - 1;
    while( x -- )
    {
        long a, b;
        ll koszt;
        cin >> a >> b >> koszt;
        wartosci_sciezki[ numer_sciezki ] = koszt;
        graf[ a ].pb(make_pair( b, numer_sciezki ));
        graf[ b ].pb(make_pair( a, numer_sciezki ));
        numer_sciezki ++;
    }
#ifdef TEST
    for( long i = 1; i <= n; i ++ )
    {
        cout << i << ":";
        for( auto nei: graf[ i ] )
            cout << "(" << nei.first << "#"<< nei.second << ") ";
        cout << endl;
    }

    for( long i = 0; i < numer_sciezki; i ++ )
    {
        cout << i << ":" << wartosci_sciezki [ i ];
        cout << endl;
    }
#endif // TEST

    for( long wezel = 1; wezel <=n; wezel ++ )
    {
        queue <long> trasa;
        while(!trasa.empty()) trasa.pop();
        for( long i = 0; i < MAXN; i ++  ) visited_node[ i ] = 0;
        szukaj_tras( wezel, trasa, 0 );
    }
#ifdef TEST
     cout << "trasy:\n";
     for( long i = 0; i < trasy.size(); i ++ )
     {
         cout << i << ": ";
         for( auto tt: trasy[ i ] )
             cout << tt << " ";
         cout << endl;
     }
     cout << "wartosci tras:\n";
     for( long i = 0; i < wartosci_tras.size(); i ++ )
         cout << i << ": " << wartosci_tras[ i ] << endl;
     cout << "sciezki_trasy:\n";
     for( long i = 0; i < n - 1; i ++ )
     {
         cout << i << ": ";
         for( auto tt: sciezki_trasy[ i ] )
             cout << tt << " ";
         cout << endl;
     }
#endif // TEST
    for( long nr_trasy = 0 ; nr_trasy < trasy.size(); nr_trasy ++ )
    {
        for( long i = 0; i < trasy_visited.size(); i ++  ) trasy_visited[ i ] = 0;
        zestawy_tras( nr_trasy, 0 );
    }

    cout << max_wartosc << '\n';
    return 0;
}