#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 300500;
int n, m;
int p[N];
ll c[N];
vector<int> g[N];
ll base[N];
ll d[N];
priority_queue<ll, vector<ll>> q[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i < n + m; ++i) {
cin >> p[i] >> c[i];
--p[i];
g[p[i]].push_back(i);
}
c[0] = 0;
for (int i = n; i < n + m; ++i) {
base[i] = c[i];
d[i] = -1;
q[i].push(c[i]);
q[i].push(c[i]);
}
for (int i = n - 1; i >= 0; --i) {
for (int j : g[i]) {
while (!q[j].empty()) {
q[i].push(q[j].top());
q[j].pop();
}
base[i] += base[j];
d[i] += d[j];
}
while (d[i] + q[i].size() > 1) {
q[i].pop();
}
ll x = q[i].top();
q[i].pop();
ll y = q[i].top();
q[i].pop();
q[i].push(x + c[i]);
q[i].push(y + c[i]);
base[i] += c[i];
}
vector<ll> a;
while (!q[0].empty()) {
a.push_back(q[0].top());
q[0].pop();
}
reverse(a.begin(), a.end());
ll ans = base[0];
ll diff = d[0];
if (diff < 0) {
ans += diff * a[0];
}
for (int i = 0; i < (int)a.size() - 1; ++i) {
diff += 1;
ans += diff * (a[i + 1] - a[i]);
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16816 KB |
Output is correct |
2 |
Correct |
13 ms |
16812 KB |
Output is correct |
3 |
Correct |
11 ms |
16808 KB |
Output is correct |
4 |
Correct |
16 ms |
16724 KB |
Output is correct |
5 |
Correct |
10 ms |
16812 KB |
Output is correct |
6 |
Correct |
10 ms |
16724 KB |
Output is correct |
7 |
Correct |
13 ms |
16796 KB |
Output is correct |
8 |
Correct |
12 ms |
16812 KB |
Output is correct |
9 |
Correct |
13 ms |
16812 KB |
Output is correct |
10 |
Correct |
12 ms |
16812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16808 KB |
Output is correct |
2 |
Correct |
11 ms |
16712 KB |
Output is correct |
3 |
Correct |
12 ms |
16812 KB |
Output is correct |
4 |
Correct |
11 ms |
16752 KB |
Output is correct |
5 |
Correct |
12 ms |
16784 KB |
Output is correct |
6 |
Correct |
13 ms |
16808 KB |
Output is correct |
7 |
Correct |
13 ms |
16808 KB |
Output is correct |
8 |
Correct |
13 ms |
16812 KB |
Output is correct |
9 |
Correct |
12 ms |
16808 KB |
Output is correct |
10 |
Correct |
12 ms |
16812 KB |
Output is correct |
11 |
Correct |
12 ms |
16792 KB |
Output is correct |
12 |
Correct |
11 ms |
16784 KB |
Output is correct |
13 |
Correct |
13 ms |
16820 KB |
Output is correct |
14 |
Correct |
11 ms |
16808 KB |
Output is correct |
15 |
Correct |
11 ms |
16796 KB |
Output is correct |
16 |
Correct |
11 ms |
16852 KB |
Output is correct |
17 |
Correct |
12 ms |
16748 KB |
Output is correct |
18 |
Correct |
13 ms |
16852 KB |
Output is correct |
19 |
Correct |
12 ms |
16824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16816 KB |
Output is correct |
2 |
Correct |
13 ms |
16812 KB |
Output is correct |
3 |
Correct |
11 ms |
16808 KB |
Output is correct |
4 |
Correct |
16 ms |
16724 KB |
Output is correct |
5 |
Correct |
10 ms |
16812 KB |
Output is correct |
6 |
Correct |
10 ms |
16724 KB |
Output is correct |
7 |
Correct |
13 ms |
16796 KB |
Output is correct |
8 |
Correct |
12 ms |
16812 KB |
Output is correct |
9 |
Correct |
13 ms |
16812 KB |
Output is correct |
10 |
Correct |
12 ms |
16812 KB |
Output is correct |
11 |
Correct |
11 ms |
16808 KB |
Output is correct |
12 |
Correct |
11 ms |
16712 KB |
Output is correct |
13 |
Correct |
12 ms |
16812 KB |
Output is correct |
14 |
Correct |
11 ms |
16752 KB |
Output is correct |
15 |
Correct |
12 ms |
16784 KB |
Output is correct |
16 |
Correct |
13 ms |
16808 KB |
Output is correct |
17 |
Correct |
13 ms |
16808 KB |
Output is correct |
18 |
Correct |
13 ms |
16812 KB |
Output is correct |
19 |
Correct |
12 ms |
16808 KB |
Output is correct |
20 |
Correct |
12 ms |
16812 KB |
Output is correct |
21 |
Correct |
12 ms |
16792 KB |
Output is correct |
22 |
Correct |
11 ms |
16784 KB |
Output is correct |
23 |
Correct |
13 ms |
16820 KB |
Output is correct |
24 |
Correct |
11 ms |
16808 KB |
Output is correct |
25 |
Correct |
11 ms |
16796 KB |
Output is correct |
26 |
Correct |
11 ms |
16852 KB |
Output is correct |
27 |
Correct |
12 ms |
16748 KB |
Output is correct |
28 |
Correct |
13 ms |
16852 KB |
Output is correct |
29 |
Correct |
12 ms |
16824 KB |
Output is correct |
30 |
Correct |
12 ms |
16852 KB |
Output is correct |
31 |
Correct |
17 ms |
17104 KB |
Output is correct |
32 |
Correct |
16 ms |
17108 KB |
Output is correct |
33 |
Correct |
18 ms |
17412 KB |
Output is correct |
34 |
Correct |
18 ms |
17608 KB |
Output is correct |
35 |
Correct |
21 ms |
18352 KB |
Output is correct |
36 |
Correct |
19 ms |
18184 KB |
Output is correct |
37 |
Correct |
24 ms |
18072 KB |
Output is correct |
38 |
Correct |
39 ms |
19556 KB |
Output is correct |
39 |
Correct |
25 ms |
18400 KB |
Output is correct |
40 |
Correct |
17 ms |
17316 KB |
Output is correct |
41 |
Correct |
13 ms |
17236 KB |
Output is correct |
42 |
Correct |
12 ms |
17236 KB |
Output is correct |
43 |
Correct |
229 ms |
47932 KB |
Output is correct |
44 |
Correct |
208 ms |
39840 KB |
Output is correct |
45 |
Correct |
174 ms |
39456 KB |
Output is correct |
46 |
Correct |
13 ms |
17492 KB |
Output is correct |
47 |
Correct |
17 ms |
17392 KB |
Output is correct |
48 |
Correct |
16 ms |
17336 KB |
Output is correct |
49 |
Correct |
15 ms |
17364 KB |
Output is correct |
50 |
Correct |
13 ms |
17328 KB |
Output is correct |
51 |
Correct |
15 ms |
17212 KB |
Output is correct |
52 |
Correct |
22 ms |
17860 KB |
Output is correct |
53 |
Correct |
15 ms |
17532 KB |
Output is correct |
54 |
Correct |
13 ms |
17368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16816 KB |
Output is correct |
2 |
Correct |
13 ms |
16812 KB |
Output is correct |
3 |
Correct |
11 ms |
16808 KB |
Output is correct |
4 |
Correct |
16 ms |
16724 KB |
Output is correct |
5 |
Correct |
10 ms |
16812 KB |
Output is correct |
6 |
Correct |
10 ms |
16724 KB |
Output is correct |
7 |
Correct |
13 ms |
16796 KB |
Output is correct |
8 |
Correct |
12 ms |
16812 KB |
Output is correct |
9 |
Correct |
13 ms |
16812 KB |
Output is correct |
10 |
Correct |
12 ms |
16812 KB |
Output is correct |
11 |
Correct |
11 ms |
16808 KB |
Output is correct |
12 |
Correct |
11 ms |
16712 KB |
Output is correct |
13 |
Correct |
12 ms |
16812 KB |
Output is correct |
14 |
Correct |
11 ms |
16752 KB |
Output is correct |
15 |
Correct |
12 ms |
16784 KB |
Output is correct |
16 |
Correct |
13 ms |
16808 KB |
Output is correct |
17 |
Correct |
13 ms |
16808 KB |
Output is correct |
18 |
Correct |
13 ms |
16812 KB |
Output is correct |
19 |
Correct |
12 ms |
16808 KB |
Output is correct |
20 |
Correct |
12 ms |
16812 KB |
Output is correct |
21 |
Correct |
12 ms |
16792 KB |
Output is correct |
22 |
Correct |
11 ms |
16784 KB |
Output is correct |
23 |
Correct |
13 ms |
16820 KB |
Output is correct |
24 |
Correct |
11 ms |
16808 KB |
Output is correct |
25 |
Correct |
11 ms |
16796 KB |
Output is correct |
26 |
Correct |
11 ms |
16852 KB |
Output is correct |
27 |
Correct |
12 ms |
16748 KB |
Output is correct |
28 |
Correct |
13 ms |
16852 KB |
Output is correct |
29 |
Correct |
12 ms |
16824 KB |
Output is correct |
30 |
Correct |
12 ms |
16852 KB |
Output is correct |
31 |
Correct |
17 ms |
17104 KB |
Output is correct |
32 |
Correct |
16 ms |
17108 KB |
Output is correct |
33 |
Correct |
18 ms |
17412 KB |
Output is correct |
34 |
Correct |
18 ms |
17608 KB |
Output is correct |
35 |
Correct |
21 ms |
18352 KB |
Output is correct |
36 |
Correct |
19 ms |
18184 KB |
Output is correct |
37 |
Correct |
24 ms |
18072 KB |
Output is correct |
38 |
Correct |
39 ms |
19556 KB |
Output is correct |
39 |
Correct |
25 ms |
18400 KB |
Output is correct |
40 |
Correct |
17 ms |
17316 KB |
Output is correct |
41 |
Correct |
13 ms |
17236 KB |
Output is correct |
42 |
Correct |
12 ms |
17236 KB |
Output is correct |
43 |
Correct |
229 ms |
47932 KB |
Output is correct |
44 |
Correct |
208 ms |
39840 KB |
Output is correct |
45 |
Correct |
174 ms |
39456 KB |
Output is correct |
46 |
Correct |
13 ms |
17492 KB |
Output is correct |
47 |
Correct |
17 ms |
17392 KB |
Output is correct |
48 |
Correct |
16 ms |
17336 KB |
Output is correct |
49 |
Correct |
15 ms |
17364 KB |
Output is correct |
50 |
Correct |
13 ms |
17328 KB |
Output is correct |
51 |
Correct |
15 ms |
17212 KB |
Output is correct |
52 |
Correct |
22 ms |
17860 KB |
Output is correct |
53 |
Correct |
15 ms |
17532 KB |
Output is correct |
54 |
Correct |
13 ms |
17368 KB |
Output is correct |
55 |
Correct |
68 ms |
23956 KB |
Output is correct |
56 |
Correct |
505 ms |
71680 KB |
Output is correct |
57 |
Correct |
1010 ms |
124568 KB |
Output is correct |
58 |
Correct |
1173 ms |
147692 KB |
Output is correct |
59 |
Execution timed out |
2066 ms |
241116 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |