//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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
5 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7368 KB |
Output is correct |
5 |
Correct |
4 ms |
7364 KB |
Output is correct |
6 |
Correct |
4 ms |
7364 KB |
Output is correct |
7 |
Correct |
5 ms |
7364 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Incorrect |
4 ms |
7252 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
5 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7368 KB |
Output is correct |
5 |
Correct |
4 ms |
7364 KB |
Output is correct |
6 |
Correct |
4 ms |
7364 KB |
Output is correct |
7 |
Correct |
5 ms |
7364 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Correct |
4 ms |
7252 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Incorrect |
4 ms |
7252 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
5 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7368 KB |
Output is correct |
5 |
Correct |
4 ms |
7364 KB |
Output is correct |
6 |
Correct |
4 ms |
7364 KB |
Output is correct |
7 |
Correct |
5 ms |
7364 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Correct |
4 ms |
7252 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Incorrect |
4 ms |
7252 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |