이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > child[300005];
long long n, m, ans;
pair<long long, long long> dfs(long long u)
{
long long sz=child[u].size();
if (!sz)
return {0, 0};
vector<pair<long long, long long> > vec2;
vector<long long> vec;
for (long long i=0; i<sz; i++)
{
long long v=child[u][i].first, w=child[u][i].second;
pair<long long, long long> res=dfs(v);
vec2.push_back({res.first+w, res.second+w});
vec.push_back(res.first+w);
vec.push_back(res.second+w);
}
sort(vec.begin(), vec.end());
for (long long i=0; i<sz; i++)
ans+=max(0LL, max(vec2[i].first-vec[sz], vec[sz]-vec2[i].second));
return {vec[sz-1], vec[sz]};
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (long long i=2; i<=n+m; i++)
{
long long p, c;
cin >> p >> c;
child[p].push_back({i, c});
}
dfs(1);
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |