#include <bits/stdc++.h>
using namespace std;
#define int ll
#define endl '\n' //comment for interactive
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define re resize
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define all1(x) (x).begin()+1, (x).end()
#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, n) for(int i = 1; i <= n; i++)
#define print(x) cout << #x << ": " << x << endl << flush
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
typedef array<int, 3> ti;
typedef vector<ii> vii;
typedef vector<ti> vti;
typedef vector<vi> vvi;
typedef priority_queue<int> pq;
template<class T> bool ckmin(T&a, T b) { bool B = a > b; a = min(a, b); return B; }
template<class T> bool ckmax(T&a, T b) { bool B = a < b; a = max(a, b); return B; }
const int inf = 1e17;
const int maxn = 3e5 + 5;
int p[maxn];
int c[maxn];
vi adj[maxn];
struct slp {
priority_queue<int> pq;
int m = 0, c = 0;
};
slp dfs(int u) {
slp slp1;
for(auto v : adj[u]) {
slp slp2 = dfs(v);
if(slp1.pq.size() < slp2.pq.size()) swap(slp1, slp2);
slp1.m += slp2.m;
slp1.c += slp2.c;
while(!slp2.pq.empty()) {
int x = slp2.pq.top();
slp2.pq.pop();
slp1.pq.push(x);
}
}
if(adj[u].size() == 0) {
slp1.m = -1;
slp1.c = c[u];
slp1.pq.push(c[u]);
slp1.pq.push(c[u]);
return slp1;
}
int x = slp1.m + slp1.pq.size();
loop(i, x-1)
slp1.pq.pop();
slp1.c += c[u];
int a = slp1.pq.top();
slp1.pq.pop();
int b = slp1.pq.top();
slp1.pq.pop();
slp1.pq.push(a + c[u]);
slp1.pq.push(b + c[u]);
return slp1;
}
void solve() {
int n, m;
cin >> n >> m;
loop1(i, n+m) if(i != 1) {
cin >> p[i] >> c[i];
adj[p[i]].pb(i);
}
slp res = dfs(1);
int mr = res.m;
int cr = res.c;
vi pos;
while(!res.pq.empty()) {
pos.pb(res.pq.top());
res.pq.pop();
}
pos.pb(0);
reverse(all(pos));
int ans = cr;
for(int i = 0; i < -mr; i++) {
ans += (mr + i) * (pos[i+1] - pos[i]);
}
cout << ans << endl;
}
signed main() {
fast_io;
int t = 1; //cin >> t;
while(t--)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10840 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
1 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10852 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
1 ms |
10844 KB |
Output is correct |
8 |
Correct |
1 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
1 ms |
10844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
1 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10860 KB |
Output is correct |
6 |
Correct |
1 ms |
10844 KB |
Output is correct |
7 |
Correct |
1 ms |
10844 KB |
Output is correct |
8 |
Correct |
1 ms |
10844 KB |
Output is correct |
9 |
Correct |
1 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10840 KB |
Output is correct |
11 |
Correct |
1 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
1 ms |
10844 KB |
Output is correct |
15 |
Correct |
1 ms |
10896 KB |
Output is correct |
16 |
Correct |
1 ms |
10844 KB |
Output is correct |
17 |
Correct |
1 ms |
10844 KB |
Output is correct |
18 |
Correct |
2 ms |
10844 KB |
Output is correct |
19 |
Correct |
1 ms |
10844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10840 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
1 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10852 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
1 ms |
10844 KB |
Output is correct |
8 |
Correct |
1 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
1 ms |
10844 KB |
Output is correct |
11 |
Correct |
1 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
1 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10860 KB |
Output is correct |
16 |
Correct |
1 ms |
10844 KB |
Output is correct |
17 |
Correct |
1 ms |
10844 KB |
Output is correct |
18 |
Correct |
1 ms |
10844 KB |
Output is correct |
19 |
Correct |
1 ms |
10844 KB |
Output is correct |
20 |
Correct |
2 ms |
10840 KB |
Output is correct |
21 |
Correct |
1 ms |
10844 KB |
Output is correct |
22 |
Correct |
2 ms |
10844 KB |
Output is correct |
23 |
Correct |
2 ms |
10844 KB |
Output is correct |
24 |
Correct |
1 ms |
10844 KB |
Output is correct |
25 |
Correct |
1 ms |
10896 KB |
Output is correct |
26 |
Correct |
1 ms |
10844 KB |
Output is correct |
27 |
Correct |
1 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
10844 KB |
Output is correct |
29 |
Correct |
1 ms |
10844 KB |
Output is correct |
30 |
Correct |
2 ms |
10844 KB |
Output is correct |
31 |
Correct |
2 ms |
10844 KB |
Output is correct |
32 |
Correct |
2 ms |
10844 KB |
Output is correct |
33 |
Correct |
2 ms |
10844 KB |
Output is correct |
34 |
Correct |
3 ms |
10844 KB |
Output is correct |
35 |
Correct |
2 ms |
10844 KB |
Output is correct |
36 |
Correct |
3 ms |
10844 KB |
Output is correct |
37 |
Correct |
3 ms |
10844 KB |
Output is correct |
38 |
Correct |
3 ms |
10840 KB |
Output is correct |
39 |
Correct |
3 ms |
10840 KB |
Output is correct |
40 |
Correct |
2 ms |
11868 KB |
Output is correct |
41 |
Correct |
4 ms |
11868 KB |
Output is correct |
42 |
Correct |
2 ms |
10844 KB |
Output is correct |
43 |
Correct |
3 ms |
11356 KB |
Output is correct |
44 |
Correct |
3 ms |
11356 KB |
Output is correct |
45 |
Correct |
4 ms |
11272 KB |
Output is correct |
46 |
Correct |
3 ms |
11056 KB |
Output is correct |
47 |
Correct |
3 ms |
11100 KB |
Output is correct |
48 |
Correct |
3 ms |
11100 KB |
Output is correct |
49 |
Correct |
3 ms |
11100 KB |
Output is correct |
50 |
Correct |
3 ms |
10840 KB |
Output is correct |
51 |
Correct |
4 ms |
10844 KB |
Output is correct |
52 |
Correct |
3 ms |
11004 KB |
Output is correct |
53 |
Correct |
3 ms |
11100 KB |
Output is correct |
54 |
Correct |
3 ms |
11020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10840 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
1 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10852 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
1 ms |
10844 KB |
Output is correct |
8 |
Correct |
1 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
1 ms |
10844 KB |
Output is correct |
11 |
Correct |
1 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
1 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10860 KB |
Output is correct |
16 |
Correct |
1 ms |
10844 KB |
Output is correct |
17 |
Correct |
1 ms |
10844 KB |
Output is correct |
18 |
Correct |
1 ms |
10844 KB |
Output is correct |
19 |
Correct |
1 ms |
10844 KB |
Output is correct |
20 |
Correct |
2 ms |
10840 KB |
Output is correct |
21 |
Correct |
1 ms |
10844 KB |
Output is correct |
22 |
Correct |
2 ms |
10844 KB |
Output is correct |
23 |
Correct |
2 ms |
10844 KB |
Output is correct |
24 |
Correct |
1 ms |
10844 KB |
Output is correct |
25 |
Correct |
1 ms |
10896 KB |
Output is correct |
26 |
Correct |
1 ms |
10844 KB |
Output is correct |
27 |
Correct |
1 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
10844 KB |
Output is correct |
29 |
Correct |
1 ms |
10844 KB |
Output is correct |
30 |
Correct |
2 ms |
10844 KB |
Output is correct |
31 |
Correct |
2 ms |
10844 KB |
Output is correct |
32 |
Correct |
2 ms |
10844 KB |
Output is correct |
33 |
Correct |
2 ms |
10844 KB |
Output is correct |
34 |
Correct |
3 ms |
10844 KB |
Output is correct |
35 |
Correct |
2 ms |
10844 KB |
Output is correct |
36 |
Correct |
3 ms |
10844 KB |
Output is correct |
37 |
Correct |
3 ms |
10844 KB |
Output is correct |
38 |
Correct |
3 ms |
10840 KB |
Output is correct |
39 |
Correct |
3 ms |
10840 KB |
Output is correct |
40 |
Correct |
2 ms |
11868 KB |
Output is correct |
41 |
Correct |
4 ms |
11868 KB |
Output is correct |
42 |
Correct |
2 ms |
10844 KB |
Output is correct |
43 |
Correct |
3 ms |
11356 KB |
Output is correct |
44 |
Correct |
3 ms |
11356 KB |
Output is correct |
45 |
Correct |
4 ms |
11272 KB |
Output is correct |
46 |
Correct |
3 ms |
11056 KB |
Output is correct |
47 |
Correct |
3 ms |
11100 KB |
Output is correct |
48 |
Correct |
3 ms |
11100 KB |
Output is correct |
49 |
Correct |
3 ms |
11100 KB |
Output is correct |
50 |
Correct |
3 ms |
10840 KB |
Output is correct |
51 |
Correct |
4 ms |
10844 KB |
Output is correct |
52 |
Correct |
3 ms |
11004 KB |
Output is correct |
53 |
Correct |
3 ms |
11100 KB |
Output is correct |
54 |
Correct |
3 ms |
11020 KB |
Output is correct |
55 |
Correct |
5 ms |
11356 KB |
Output is correct |
56 |
Correct |
17 ms |
12500 KB |
Output is correct |
57 |
Correct |
32 ms |
13380 KB |
Output is correct |
58 |
Correct |
36 ms |
14252 KB |
Output is correct |
59 |
Correct |
50 ms |
15200 KB |
Output is correct |
60 |
Correct |
62 ms |
16412 KB |
Output is correct |
61 |
Correct |
71 ms |
17932 KB |
Output is correct |
62 |
Correct |
76 ms |
18676 KB |
Output is correct |
63 |
Correct |
92 ms |
20004 KB |
Output is correct |
64 |
Correct |
98 ms |
20528 KB |
Output is correct |
65 |
Correct |
88 ms |
82436 KB |
Output is correct |
66 |
Correct |
92 ms |
82512 KB |
Output is correct |
67 |
Correct |
61 ms |
21576 KB |
Output is correct |
68 |
Correct |
111 ms |
51648 KB |
Output is correct |
69 |
Correct |
122 ms |
47040 KB |
Output is correct |
70 |
Correct |
125 ms |
47036 KB |
Output is correct |
71 |
Correct |
128 ms |
24120 KB |
Output is correct |
72 |
Correct |
151 ms |
26472 KB |
Output is correct |
73 |
Correct |
129 ms |
26072 KB |
Output is correct |
74 |
Correct |
117 ms |
24496 KB |
Output is correct |
75 |
Correct |
119 ms |
23688 KB |
Output is correct |
76 |
Correct |
120 ms |
26152 KB |
Output is correct |
77 |
Correct |
130 ms |
24296 KB |
Output is correct |
78 |
Correct |
124 ms |
23584 KB |
Output is correct |
79 |
Correct |
119 ms |
28368 KB |
Output is correct |