#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
const int mxn = 300000;
int n, m;
ll d[mxn], f[mxn], p[mxn], dp[mxn];
priority_queue<ll> pq[mxn];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 1; i < n + m; i++){
cin >> p[i] >> f[i];
d[--p[i]]++;
if(i >= n){
dp[p[i]] -= f[i];
for(int j = 0; j < 2; j++) pq[p[i]].push(f[i]);
}
}
for(int i = n - 1; i; i--){
for(int j = 0; j < d[i] - 1; j++) dp[i] += pq[i].top(), pq[i].pop();
ll x[2];
for(int j = 0; j < 2; j++) x[j] = pq[i].top(), pq[i].pop();
for(int j = 0; j < 2; j++) pq[i].push(x[j] + f[i]);
dp[i] -= f[i], dp[p[i]] += dp[i];
if(pq[p[i]].size() < pq[i].size()) swap(pq[p[i]], pq[i]);
while(!pq[i].empty()) pq[p[i]].push(pq[i].top()), pq[i].pop();
}
ll ret = dp[0];
for(int i = 0; i < d[0]; i++) ret += pq[0].top(), pq[0].pop();
cout << ret << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
6 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
6 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
6 ms |
9708 KB |
Output is correct |
9 |
Correct |
6 ms |
9708 KB |
Output is correct |
10 |
Correct |
6 ms |
9708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
6 ms |
9708 KB |
Output is correct |
3 |
Correct |
6 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9836 KB |
Output is correct |
8 |
Correct |
7 ms |
9836 KB |
Output is correct |
9 |
Correct |
7 ms |
9836 KB |
Output is correct |
10 |
Correct |
6 ms |
9836 KB |
Output is correct |
11 |
Correct |
7 ms |
9836 KB |
Output is correct |
12 |
Correct |
7 ms |
9964 KB |
Output is correct |
13 |
Correct |
7 ms |
9836 KB |
Output is correct |
14 |
Correct |
7 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9852 KB |
Output is correct |
16 |
Correct |
7 ms |
9836 KB |
Output is correct |
17 |
Correct |
7 ms |
9836 KB |
Output is correct |
18 |
Correct |
7 ms |
9836 KB |
Output is correct |
19 |
Correct |
7 ms |
9836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
6 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
6 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
6 ms |
9708 KB |
Output is correct |
9 |
Correct |
6 ms |
9708 KB |
Output is correct |
10 |
Correct |
6 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9708 KB |
Output is correct |
12 |
Correct |
6 ms |
9708 KB |
Output is correct |
13 |
Correct |
6 ms |
9708 KB |
Output is correct |
14 |
Correct |
7 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9708 KB |
Output is correct |
16 |
Correct |
7 ms |
9708 KB |
Output is correct |
17 |
Correct |
7 ms |
9836 KB |
Output is correct |
18 |
Correct |
7 ms |
9836 KB |
Output is correct |
19 |
Correct |
7 ms |
9836 KB |
Output is correct |
20 |
Correct |
6 ms |
9836 KB |
Output is correct |
21 |
Correct |
7 ms |
9836 KB |
Output is correct |
22 |
Correct |
7 ms |
9964 KB |
Output is correct |
23 |
Correct |
7 ms |
9836 KB |
Output is correct |
24 |
Correct |
7 ms |
9708 KB |
Output is correct |
25 |
Correct |
7 ms |
9852 KB |
Output is correct |
26 |
Correct |
7 ms |
9836 KB |
Output is correct |
27 |
Correct |
7 ms |
9836 KB |
Output is correct |
28 |
Correct |
7 ms |
9836 KB |
Output is correct |
29 |
Correct |
7 ms |
9836 KB |
Output is correct |
30 |
Correct |
6 ms |
9836 KB |
Output is correct |
31 |
Correct |
7 ms |
9836 KB |
Output is correct |
32 |
Correct |
7 ms |
9836 KB |
Output is correct |
33 |
Correct |
8 ms |
9964 KB |
Output is correct |
34 |
Correct |
8 ms |
9964 KB |
Output is correct |
35 |
Correct |
8 ms |
9964 KB |
Output is correct |
36 |
Correct |
8 ms |
10092 KB |
Output is correct |
37 |
Correct |
9 ms |
10092 KB |
Output is correct |
38 |
Correct |
9 ms |
10092 KB |
Output is correct |
39 |
Correct |
9 ms |
10092 KB |
Output is correct |
40 |
Correct |
8 ms |
9964 KB |
Output is correct |
41 |
Correct |
8 ms |
9964 KB |
Output is correct |
42 |
Correct |
8 ms |
9964 KB |
Output is correct |
43 |
Correct |
9 ms |
10092 KB |
Output is correct |
44 |
Correct |
10 ms |
10112 KB |
Output is correct |
45 |
Correct |
9 ms |
10092 KB |
Output is correct |
46 |
Correct |
9 ms |
10092 KB |
Output is correct |
47 |
Correct |
8 ms |
10092 KB |
Output is correct |
48 |
Correct |
8 ms |
10092 KB |
Output is correct |
49 |
Correct |
10 ms |
10092 KB |
Output is correct |
50 |
Correct |
9 ms |
10072 KB |
Output is correct |
51 |
Correct |
8 ms |
9964 KB |
Output is correct |
52 |
Correct |
8 ms |
10092 KB |
Output is correct |
53 |
Correct |
9 ms |
10092 KB |
Output is correct |
54 |
Correct |
9 ms |
10092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
6 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
6 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
6 ms |
9708 KB |
Output is correct |
9 |
Correct |
6 ms |
9708 KB |
Output is correct |
10 |
Correct |
6 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9708 KB |
Output is correct |
12 |
Correct |
6 ms |
9708 KB |
Output is correct |
13 |
Correct |
6 ms |
9708 KB |
Output is correct |
14 |
Correct |
7 ms |
9708 KB |
Output is correct |
15 |
Correct |
7 ms |
9708 KB |
Output is correct |
16 |
Correct |
7 ms |
9708 KB |
Output is correct |
17 |
Correct |
7 ms |
9836 KB |
Output is correct |
18 |
Correct |
7 ms |
9836 KB |
Output is correct |
19 |
Correct |
7 ms |
9836 KB |
Output is correct |
20 |
Correct |
6 ms |
9836 KB |
Output is correct |
21 |
Correct |
7 ms |
9836 KB |
Output is correct |
22 |
Correct |
7 ms |
9964 KB |
Output is correct |
23 |
Correct |
7 ms |
9836 KB |
Output is correct |
24 |
Correct |
7 ms |
9708 KB |
Output is correct |
25 |
Correct |
7 ms |
9852 KB |
Output is correct |
26 |
Correct |
7 ms |
9836 KB |
Output is correct |
27 |
Correct |
7 ms |
9836 KB |
Output is correct |
28 |
Correct |
7 ms |
9836 KB |
Output is correct |
29 |
Correct |
7 ms |
9836 KB |
Output is correct |
30 |
Correct |
6 ms |
9836 KB |
Output is correct |
31 |
Correct |
7 ms |
9836 KB |
Output is correct |
32 |
Correct |
7 ms |
9836 KB |
Output is correct |
33 |
Correct |
8 ms |
9964 KB |
Output is correct |
34 |
Correct |
8 ms |
9964 KB |
Output is correct |
35 |
Correct |
8 ms |
9964 KB |
Output is correct |
36 |
Correct |
8 ms |
10092 KB |
Output is correct |
37 |
Correct |
9 ms |
10092 KB |
Output is correct |
38 |
Correct |
9 ms |
10092 KB |
Output is correct |
39 |
Correct |
9 ms |
10092 KB |
Output is correct |
40 |
Correct |
8 ms |
9964 KB |
Output is correct |
41 |
Correct |
8 ms |
9964 KB |
Output is correct |
42 |
Correct |
8 ms |
9964 KB |
Output is correct |
43 |
Correct |
9 ms |
10092 KB |
Output is correct |
44 |
Correct |
10 ms |
10112 KB |
Output is correct |
45 |
Correct |
9 ms |
10092 KB |
Output is correct |
46 |
Correct |
9 ms |
10092 KB |
Output is correct |
47 |
Correct |
8 ms |
10092 KB |
Output is correct |
48 |
Correct |
8 ms |
10092 KB |
Output is correct |
49 |
Correct |
10 ms |
10092 KB |
Output is correct |
50 |
Correct |
9 ms |
10072 KB |
Output is correct |
51 |
Correct |
8 ms |
9964 KB |
Output is correct |
52 |
Correct |
8 ms |
10092 KB |
Output is correct |
53 |
Correct |
9 ms |
10092 KB |
Output is correct |
54 |
Correct |
9 ms |
10092 KB |
Output is correct |
55 |
Correct |
13 ms |
10732 KB |
Output is correct |
56 |
Correct |
35 ms |
13548 KB |
Output is correct |
57 |
Correct |
54 ms |
16236 KB |
Output is correct |
58 |
Correct |
62 ms |
18024 KB |
Output is correct |
59 |
Correct |
84 ms |
20580 KB |
Output is correct |
60 |
Correct |
104 ms |
23144 KB |
Output is correct |
61 |
Correct |
117 ms |
25572 KB |
Output is correct |
62 |
Correct |
135 ms |
26976 KB |
Output is correct |
63 |
Correct |
170 ms |
30168 KB |
Output is correct |
64 |
Correct |
169 ms |
31204 KB |
Output is correct |
65 |
Correct |
88 ms |
24044 KB |
Output is correct |
66 |
Correct |
87 ms |
24044 KB |
Output is correct |
67 |
Correct |
104 ms |
24044 KB |
Output is correct |
68 |
Correct |
153 ms |
28384 KB |
Output is correct |
69 |
Correct |
157 ms |
28388 KB |
Output is correct |
70 |
Correct |
159 ms |
28260 KB |
Output is correct |
71 |
Correct |
169 ms |
32468 KB |
Output is correct |
72 |
Correct |
165 ms |
32468 KB |
Output is correct |
73 |
Correct |
130 ms |
28784 KB |
Output is correct |
74 |
Correct |
138 ms |
29456 KB |
Output is correct |
75 |
Correct |
137 ms |
27892 KB |
Output is correct |
76 |
Correct |
139 ms |
27952 KB |
Output is correct |
77 |
Correct |
151 ms |
28080 KB |
Output is correct |
78 |
Correct |
148 ms |
27876 KB |
Output is correct |
79 |
Correct |
129 ms |
25436 KB |
Output is correct |