// And you curse yourself for things you never done
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 1e4 + 10, mod = 1e9 + 7; /////////
const ll inf = 1e18 + 10;
vector<pii> v[maxn];
ll ans[2][maxn], dp[2][maxn]; // 0 up 1 down
void dfs(int u, int par = -1){
ll mx1 = -inf, mx2 = -inf;
for(auto [y, c] : v[u]){
if(y != par){
dfs(y, u);
ll x = max(ans[0][y], dp[0][y] + c);
ans[0][u]+= x;
dp[0][u]+= x;
mx1 = max(mx1, c + ans[0][y] - x);
dp[1][u]+= x;
mx2 = max(mx2, c + ans[1][y] - x);
}
}
dp[0][u]+= mx1;
dp[1][u]+= mx2;
return;/////
ll sm = 0;
ll mx = -inf;
vector<pair<ll, int> > v1, v2;
for(auto [y, c] : v[u]){
if(y != par){
ll x = max(ans[0][y], dp[0][y] + c);
sm+= x;
mx = max(mx, max(dp[1][y] + c, ans[1][y]) -x);
v1.PB({ans[1][y] + c - x, y});
v2.PB({ans[0][y] + c - x, y});
}
}
sort(v1.begin(), v1.end());
reverse(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
reverse(v2.begin(), v2.end());
for(int i = 0; i < min(2, sz(v1)); i++)
for(int j = 0; j < min(2, sz(v2)); j++)
if(v1[i].S != v2[j].S)
ans[1][u] = max(ans[1][u], sm + v1[i].F + v2[i].F);
ans[1][u] = max(ans[1][u], sm + mx);
ans[1][u] = max(ans[1][u], ans[0][u]);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();
int n;
cin >> n;
for(int i = 0; i < n-1; i++){
int a, b, c;
cin >> a >> b >> c;
v[a].PB({b, c});
v[b].PB({a, c});
}
ll ANS = 0;
for(int u = 1; u <= n; u++){
memset(ans, 0, sizeof ans);
memset(dp, 0, sizeof dp);
dfs(u);
ANS = max(ANS, ans[0][u]);
}
return cout << ANS << endl, 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
896 KB |
Output is correct |
2 |
Correct |
1 ms |
896 KB |
Output is correct |
3 |
Correct |
1 ms |
896 KB |
Output is correct |
4 |
Correct |
1 ms |
896 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
896 KB |
Output is correct |
10 |
Correct |
1 ms |
896 KB |
Output is correct |
11 |
Correct |
1 ms |
896 KB |
Output is correct |
12 |
Correct |
1 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
896 KB |
Output is correct |
2 |
Correct |
1 ms |
896 KB |
Output is correct |
3 |
Correct |
1 ms |
896 KB |
Output is correct |
4 |
Correct |
1 ms |
896 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
896 KB |
Output is correct |
10 |
Correct |
1 ms |
896 KB |
Output is correct |
11 |
Correct |
1 ms |
896 KB |
Output is correct |
12 |
Correct |
1 ms |
896 KB |
Output is correct |
13 |
Correct |
2 ms |
896 KB |
Output is correct |
14 |
Correct |
2 ms |
928 KB |
Output is correct |
15 |
Correct |
2 ms |
896 KB |
Output is correct |
16 |
Correct |
4 ms |
896 KB |
Output is correct |
17 |
Correct |
4 ms |
896 KB |
Output is correct |
18 |
Correct |
3 ms |
896 KB |
Output is correct |
19 |
Correct |
4 ms |
896 KB |
Output is correct |
20 |
Correct |
4 ms |
896 KB |
Output is correct |
21 |
Correct |
4 ms |
896 KB |
Output is correct |
22 |
Correct |
4 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
896 KB |
Output is correct |
2 |
Correct |
1 ms |
896 KB |
Output is correct |
3 |
Correct |
1 ms |
896 KB |
Output is correct |
4 |
Correct |
1 ms |
896 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
896 KB |
Output is correct |
10 |
Correct |
1 ms |
896 KB |
Output is correct |
11 |
Correct |
1 ms |
896 KB |
Output is correct |
12 |
Correct |
1 ms |
896 KB |
Output is correct |
13 |
Correct |
2 ms |
896 KB |
Output is correct |
14 |
Correct |
2 ms |
928 KB |
Output is correct |
15 |
Correct |
2 ms |
896 KB |
Output is correct |
16 |
Correct |
4 ms |
896 KB |
Output is correct |
17 |
Correct |
4 ms |
896 KB |
Output is correct |
18 |
Correct |
3 ms |
896 KB |
Output is correct |
19 |
Correct |
4 ms |
896 KB |
Output is correct |
20 |
Correct |
4 ms |
896 KB |
Output is correct |
21 |
Correct |
4 ms |
896 KB |
Output is correct |
22 |
Correct |
4 ms |
896 KB |
Output is correct |
23 |
Correct |
774 ms |
1272 KB |
Output is correct |
24 |
Correct |
758 ms |
1272 KB |
Output is correct |
25 |
Correct |
733 ms |
1192 KB |
Output is correct |
26 |
Execution timed out |
1096 ms |
1408 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
896 KB |
Output is correct |
2 |
Correct |
1 ms |
896 KB |
Output is correct |
3 |
Correct |
1 ms |
896 KB |
Output is correct |
4 |
Correct |
1 ms |
896 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
896 KB |
Output is correct |
10 |
Correct |
1 ms |
896 KB |
Output is correct |
11 |
Correct |
1 ms |
896 KB |
Output is correct |
12 |
Correct |
1 ms |
896 KB |
Output is correct |
13 |
Correct |
2 ms |
896 KB |
Output is correct |
14 |
Correct |
2 ms |
928 KB |
Output is correct |
15 |
Correct |
2 ms |
896 KB |
Output is correct |
16 |
Correct |
4 ms |
896 KB |
Output is correct |
17 |
Correct |
4 ms |
896 KB |
Output is correct |
18 |
Correct |
3 ms |
896 KB |
Output is correct |
19 |
Correct |
4 ms |
896 KB |
Output is correct |
20 |
Correct |
4 ms |
896 KB |
Output is correct |
21 |
Correct |
4 ms |
896 KB |
Output is correct |
22 |
Correct |
4 ms |
896 KB |
Output is correct |
23 |
Correct |
774 ms |
1272 KB |
Output is correct |
24 |
Correct |
758 ms |
1272 KB |
Output is correct |
25 |
Correct |
733 ms |
1192 KB |
Output is correct |
26 |
Execution timed out |
1096 ms |
1408 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |