This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T,class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 300005
#define pii pair<ll, ll>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
struct obj{
ll l, r, y; //[l, r] at y
obj(){l = r = y = 0;}
obj(ll a, ll b, ll c){l = a, r = b, y = c;}
};
vector<pii> adj[maxn];
obj dfs(int n) {
obj ret = obj();
ll ty = 0, slope = 0, curx = 0;
int chi = 0;
vector<ll> pnt;
for (auto [v, e]:adj[n]) {
chi++;
obj tmp = dfs(v);
tmp.l += e, tmp.r += e;
pnt.push_back(tmp.l), pnt.push_back(tmp.r);
ty += tmp.y + tmp.l;
}
sort(pnt.begin(), pnt.end());
slope = -chi;
for (int i = 0;i < chi;i++) {
ty += slope * (pnt[i] - curx);
slope++;
curx = pnt[i];
}
if (chi) {
//debug(n);
//pary(pnt.begin(), pnt.end());
ret.y = ty;
ret.l = pnt[chi-1], ret.r = pnt[chi];
//debug(ret.l, ret.r, ret.y);
}
return ret;
}
int main() {
io
int n, m;
cin >> n >> m;
for (int i = 2;i <= n + m;i++) {
int pi, ci;
cin >> pi >> ci;
adj[pi].push_back({i, ci});
}
obj ans = dfs(1);
cout << ans.y << "\n";
}
# | 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... |