#include <bits/stdc++.h>
#define FR(i, N) for (int i = 0; i < int(N); i++)
#define all(x) begin(x), end(x)
#define f first
#define s second
using namespace std;
using ll = long long;
const int MAXN = (int)(2e5) + 5;
vector<pair<int, int>> adj[MAXN];
int dp[MAXN][2];
multiset<int> trans[MAXN];
int sm[MAXN];
void dfs(int n, int p, int w) {
int allSum = 0;
for (auto& e : adj[n]) {
if (e.f != p) {
dfs(e.f, n, e.s);
trans[n].insert(e.s + dp[e.f][0] - dp[e.f][1]);
allSum += dp[e.f][1];
}
}
sm[n] = allSum;
dp[n][0] = allSum;
dp[n][1] = allSum + (trans[n].size() > 0 ? max(0, *trans[n].rbegin() + w): 0);
}
int ans = 0;
void sol(int n, int p) {
ans = max(dp[n][0], ans);
if (trans[n].size() >= 2) {
ans = max(ans, dp[n][0] + *trans[n].rbegin() + *next(trans[n].rbegin()));
}
ans = max(ans, dp[n][0]);
for (auto e : adj[n]) {
if (e.f != p) {
int orin0 = dp[n][0];
int orin1 = dp[n][1];
int orinsm = sm[n];
trans[n].erase(trans[n].find(e.s + dp[e.f][0] - dp[e.f][1]));
sm[n] -= dp[e.f][1];
dp[n][0] = sm[n];
dp[n][1] = sm[n] + (trans[n].size() > 0 ? max(0, *trans[n].rbegin() + e.s): 0);
int orie0 = dp[e.f][0];
int orie1 = dp[e.f][1];
int oriesm = sm[e.f];
sm[e.f] += dp[n][1];
dp[e.f][0] = sm[e.f];
dp[e.f][1] = sm[e.f] + (trans[e.f].size() > 0 ? max(0, *trans[e.f].rbegin() + e.s): 0);
trans[e.f].insert(e.s + dp[n][0] - dp[n][1]);
sol(e.f, n);
trans[e.f].erase(trans[e.f].find(e.s + dp[n][0] - dp[n][1]));
sm[n] = orinsm;
dp[n][0] = orin0;
dp[n][1] = orin1;
dp[e.f][0] = orie0;
dp[e.f][1] = orie1;
sm[e.f] = oriesm;
trans[n].insert(e.s + dp[e.f][0] - dp[e.f][1]);
}
}
}
int main() {
// freopen("input.in", "r", stdin);
cin.tie(0);
cin.sync_with_stdio(0);
int N;
cin >> N;
FR(i, N-1) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
dfs(0, 0, 0);
sol(0, 0);
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
10 ms |
14444 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
11 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14444 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14444 KB |
Output is correct |
9 |
Correct |
10 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14444 KB |
Output is correct |
12 |
Correct |
10 ms |
14444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
10 ms |
14444 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
11 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14444 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14444 KB |
Output is correct |
9 |
Correct |
10 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14444 KB |
Output is correct |
12 |
Correct |
10 ms |
14444 KB |
Output is correct |
13 |
Correct |
10 ms |
14444 KB |
Output is correct |
14 |
Correct |
10 ms |
14444 KB |
Output is correct |
15 |
Correct |
10 ms |
14444 KB |
Output is correct |
16 |
Correct |
10 ms |
14444 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
10 ms |
14444 KB |
Output is correct |
20 |
Correct |
10 ms |
14444 KB |
Output is correct |
21 |
Correct |
10 ms |
14444 KB |
Output is correct |
22 |
Correct |
10 ms |
14444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
10 ms |
14444 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
11 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14444 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14444 KB |
Output is correct |
9 |
Correct |
10 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14444 KB |
Output is correct |
12 |
Correct |
10 ms |
14444 KB |
Output is correct |
13 |
Correct |
10 ms |
14444 KB |
Output is correct |
14 |
Correct |
10 ms |
14444 KB |
Output is correct |
15 |
Correct |
10 ms |
14444 KB |
Output is correct |
16 |
Correct |
10 ms |
14444 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
10 ms |
14444 KB |
Output is correct |
20 |
Correct |
10 ms |
14444 KB |
Output is correct |
21 |
Correct |
10 ms |
14444 KB |
Output is correct |
22 |
Correct |
10 ms |
14444 KB |
Output is correct |
23 |
Correct |
16 ms |
14956 KB |
Output is correct |
24 |
Correct |
14 ms |
14976 KB |
Output is correct |
25 |
Correct |
14 ms |
14956 KB |
Output is correct |
26 |
Correct |
18 ms |
15340 KB |
Output is correct |
27 |
Correct |
19 ms |
15340 KB |
Output is correct |
28 |
Correct |
20 ms |
15468 KB |
Output is correct |
29 |
Correct |
20 ms |
15468 KB |
Output is correct |
30 |
Correct |
25 ms |
15468 KB |
Output is correct |
31 |
Correct |
19 ms |
15980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
10 ms |
14444 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
11 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14444 KB |
Output is correct |
7 |
Correct |
10 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14444 KB |
Output is correct |
9 |
Correct |
10 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14444 KB |
Output is correct |
12 |
Correct |
10 ms |
14444 KB |
Output is correct |
13 |
Correct |
10 ms |
14444 KB |
Output is correct |
14 |
Correct |
10 ms |
14444 KB |
Output is correct |
15 |
Correct |
10 ms |
14444 KB |
Output is correct |
16 |
Correct |
10 ms |
14444 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
10 ms |
14444 KB |
Output is correct |
20 |
Correct |
10 ms |
14444 KB |
Output is correct |
21 |
Correct |
10 ms |
14444 KB |
Output is correct |
22 |
Correct |
10 ms |
14444 KB |
Output is correct |
23 |
Correct |
16 ms |
14956 KB |
Output is correct |
24 |
Correct |
14 ms |
14976 KB |
Output is correct |
25 |
Correct |
14 ms |
14956 KB |
Output is correct |
26 |
Correct |
18 ms |
15340 KB |
Output is correct |
27 |
Correct |
19 ms |
15340 KB |
Output is correct |
28 |
Correct |
20 ms |
15468 KB |
Output is correct |
29 |
Correct |
20 ms |
15468 KB |
Output is correct |
30 |
Correct |
25 ms |
15468 KB |
Output is correct |
31 |
Correct |
19 ms |
15980 KB |
Output is correct |
32 |
Correct |
83 ms |
19308 KB |
Output is correct |
33 |
Correct |
65 ms |
19304 KB |
Output is correct |
34 |
Correct |
74 ms |
19428 KB |
Output is correct |
35 |
Correct |
376 ms |
33764 KB |
Output is correct |
36 |
Correct |
358 ms |
33644 KB |
Output is correct |
37 |
Correct |
358 ms |
33752 KB |
Output is correct |
38 |
Correct |
438 ms |
34012 KB |
Output is correct |
39 |
Correct |
447 ms |
34232 KB |
Output is correct |
40 |
Correct |
412 ms |
33876 KB |
Output is correct |
41 |
Correct |
423 ms |
40472 KB |
Output is correct |