#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll l[300002], r[300002], h[300002];
vector<pair<int, ll>> adj[300002];
void dfs(int x, ll p)
{
if(adj[x].empty())
{
l[x] = r[x] = p;
h[x] = 0;
return;
}
vector<ll> v;
for(auto u : adj[x])
{
int i = u.first; ll c = u.second;
dfs(i, c);
v.push_back(l[i]);
v.push_back(r[i]);
}
sort(v.begin(), v.end());
int s = adj[x].size();
l[x] = v[s-1]; r[x] = v[s];
h[x] = 0;
for(auto u : adj[x])
{
int i = u.first;
if(r[i] < l[x]) h[x] += h[i] + l[x] - r[i];
else if(l[x] < l[i]) h[x] += h[i] + l[i] - l[x];
else h[x] += h[i];
}
l[x] += p; r[x] += p;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(NULL);
int n, m; cin >> n >> m;
for(int i=2;i<=n+m;i++)
{
ll a, b; cin >> a >> b;
adj[a].push_back({i, b});
}
dfs(1, 0); cout << h[1];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7328 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7304 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7384 KB |
Output is correct |
3 |
Incorrect |
5 ms |
7380 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7328 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7304 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7384 KB |
Output is correct |
13 |
Incorrect |
5 ms |
7380 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7328 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7304 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7384 KB |
Output is correct |
13 |
Incorrect |
5 ms |
7380 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |