#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define int long long
int inf=4e18;
struct pq {
priority_queue<int, vector<int>,
greater<int>> q;
int offside=0;
void push(int val) {
q.push(val-offside);
}
int top(){return q.top()+offside;}
void pop(){q.pop();}
void move(int k){offside+=k;}
int size(){return q.size();}
};
struct node{
priority_queue<int> lr;
pq rr;
int val=0;
int sz(){return lr.size()+rr.size();}
void add(int k) {
if(!lr.size() and !rr.size()) {
lr.push(k);
rr.push(k);
}else {
int c=lr.top();
lr.pop();
lr.push(c+k);
c=rr.top();
rr=pq();
rr.push(c+k);
}
}
};
void merge(node &a, node& b) {
b.val+=a.val;
while(a.lr.size()) {
int i=a.lr.top();
a.lr.pop();
if(i<=b.rr.top()) {
b.lr.push(i);
}
else {
b.rr.push(i);
b.val+=i-b.rr.top();
b.lr.push(b.rr.top());
b.rr.pop();
}
}
while(a.rr.size()) {
int i=a.rr.top();
a.rr.pop();
if(i>=b.lr.top()) {
b.rr.push(i);
}
else {
b.lr.push(i);
b.val+=b.lr.top()-i;
b.rr.push(b.lr.top());
b.lr.pop();
}
}
}
const int N=300010;
vector<pair<int,int>> g[N];
node res[N];
void dfs(int at) {
for(auto to:g[at]) {
dfs(to.first);
res[to.first].add(to.second);
if(res[at].sz()<res[to.first].sz())
{
swap(res[at],res[to.first]);
}
merge(res[to.first], res[at]);
}
}
int32_t main()
{
int n, m;
cin >> n >> m;
for(int i =2;i<=n+m;i++) {
int p, c;
cin >> p >> c;
g[p].push_back({i,c});
}
dfs(1);
cout << res[1].val << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
30760 KB |
Output is correct |
2 |
Correct |
15 ms |
30804 KB |
Output is correct |
3 |
Correct |
15 ms |
30804 KB |
Output is correct |
4 |
Correct |
16 ms |
30784 KB |
Output is correct |
5 |
Correct |
16 ms |
30816 KB |
Output is correct |
6 |
Correct |
16 ms |
30848 KB |
Output is correct |
7 |
Correct |
19 ms |
30804 KB |
Output is correct |
8 |
Correct |
15 ms |
30804 KB |
Output is correct |
9 |
Correct |
14 ms |
30804 KB |
Output is correct |
10 |
Correct |
15 ms |
30768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
30804 KB |
Output is correct |
2 |
Correct |
19 ms |
30780 KB |
Output is correct |
3 |
Correct |
14 ms |
30804 KB |
Output is correct |
4 |
Correct |
17 ms |
30804 KB |
Output is correct |
5 |
Correct |
17 ms |
30836 KB |
Output is correct |
6 |
Correct |
16 ms |
30804 KB |
Output is correct |
7 |
Correct |
16 ms |
30796 KB |
Output is correct |
8 |
Correct |
15 ms |
30836 KB |
Output is correct |
9 |
Correct |
16 ms |
30832 KB |
Output is correct |
10 |
Correct |
15 ms |
30804 KB |
Output is correct |
11 |
Correct |
14 ms |
30772 KB |
Output is correct |
12 |
Correct |
15 ms |
30832 KB |
Output is correct |
13 |
Correct |
16 ms |
30792 KB |
Output is correct |
14 |
Correct |
15 ms |
30768 KB |
Output is correct |
15 |
Correct |
18 ms |
30788 KB |
Output is correct |
16 |
Correct |
15 ms |
30784 KB |
Output is correct |
17 |
Correct |
14 ms |
30804 KB |
Output is correct |
18 |
Correct |
15 ms |
30748 KB |
Output is correct |
19 |
Correct |
16 ms |
30784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
30760 KB |
Output is correct |
2 |
Correct |
15 ms |
30804 KB |
Output is correct |
3 |
Correct |
15 ms |
30804 KB |
Output is correct |
4 |
Correct |
16 ms |
30784 KB |
Output is correct |
5 |
Correct |
16 ms |
30816 KB |
Output is correct |
6 |
Correct |
16 ms |
30848 KB |
Output is correct |
7 |
Correct |
19 ms |
30804 KB |
Output is correct |
8 |
Correct |
15 ms |
30804 KB |
Output is correct |
9 |
Correct |
14 ms |
30804 KB |
Output is correct |
10 |
Correct |
15 ms |
30768 KB |
Output is correct |
11 |
Correct |
20 ms |
30804 KB |
Output is correct |
12 |
Correct |
19 ms |
30780 KB |
Output is correct |
13 |
Correct |
14 ms |
30804 KB |
Output is correct |
14 |
Correct |
17 ms |
30804 KB |
Output is correct |
15 |
Correct |
17 ms |
30836 KB |
Output is correct |
16 |
Correct |
16 ms |
30804 KB |
Output is correct |
17 |
Correct |
16 ms |
30796 KB |
Output is correct |
18 |
Correct |
15 ms |
30836 KB |
Output is correct |
19 |
Correct |
16 ms |
30832 KB |
Output is correct |
20 |
Correct |
15 ms |
30804 KB |
Output is correct |
21 |
Correct |
14 ms |
30772 KB |
Output is correct |
22 |
Correct |
15 ms |
30832 KB |
Output is correct |
23 |
Correct |
16 ms |
30792 KB |
Output is correct |
24 |
Correct |
15 ms |
30768 KB |
Output is correct |
25 |
Correct |
18 ms |
30788 KB |
Output is correct |
26 |
Correct |
15 ms |
30784 KB |
Output is correct |
27 |
Correct |
14 ms |
30804 KB |
Output is correct |
28 |
Correct |
15 ms |
30748 KB |
Output is correct |
29 |
Correct |
16 ms |
30784 KB |
Output is correct |
30 |
Correct |
16 ms |
30836 KB |
Output is correct |
31 |
Correct |
15 ms |
30892 KB |
Output is correct |
32 |
Correct |
16 ms |
30932 KB |
Output is correct |
33 |
Correct |
16 ms |
30924 KB |
Output is correct |
34 |
Correct |
16 ms |
31060 KB |
Output is correct |
35 |
Correct |
18 ms |
31096 KB |
Output is correct |
36 |
Correct |
21 ms |
31096 KB |
Output is correct |
37 |
Correct |
23 ms |
31104 KB |
Output is correct |
38 |
Correct |
19 ms |
31184 KB |
Output is correct |
39 |
Correct |
20 ms |
31112 KB |
Output is correct |
40 |
Correct |
20 ms |
31752 KB |
Output is correct |
41 |
Correct |
20 ms |
31820 KB |
Output is correct |
42 |
Correct |
19 ms |
30996 KB |
Output is correct |
43 |
Correct |
19 ms |
31564 KB |
Output is correct |
44 |
Correct |
19 ms |
31480 KB |
Output is correct |
45 |
Correct |
20 ms |
31384 KB |
Output is correct |
46 |
Correct |
21 ms |
31296 KB |
Output is correct |
47 |
Correct |
18 ms |
31336 KB |
Output is correct |
48 |
Correct |
19 ms |
31296 KB |
Output is correct |
49 |
Correct |
19 ms |
31316 KB |
Output is correct |
50 |
Correct |
18 ms |
31176 KB |
Output is correct |
51 |
Correct |
20 ms |
31180 KB |
Output is correct |
52 |
Correct |
19 ms |
31220 KB |
Output is correct |
53 |
Correct |
21 ms |
31276 KB |
Output is correct |
54 |
Correct |
20 ms |
31316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
30760 KB |
Output is correct |
2 |
Correct |
15 ms |
30804 KB |
Output is correct |
3 |
Correct |
15 ms |
30804 KB |
Output is correct |
4 |
Correct |
16 ms |
30784 KB |
Output is correct |
5 |
Correct |
16 ms |
30816 KB |
Output is correct |
6 |
Correct |
16 ms |
30848 KB |
Output is correct |
7 |
Correct |
19 ms |
30804 KB |
Output is correct |
8 |
Correct |
15 ms |
30804 KB |
Output is correct |
9 |
Correct |
14 ms |
30804 KB |
Output is correct |
10 |
Correct |
15 ms |
30768 KB |
Output is correct |
11 |
Correct |
20 ms |
30804 KB |
Output is correct |
12 |
Correct |
19 ms |
30780 KB |
Output is correct |
13 |
Correct |
14 ms |
30804 KB |
Output is correct |
14 |
Correct |
17 ms |
30804 KB |
Output is correct |
15 |
Correct |
17 ms |
30836 KB |
Output is correct |
16 |
Correct |
16 ms |
30804 KB |
Output is correct |
17 |
Correct |
16 ms |
30796 KB |
Output is correct |
18 |
Correct |
15 ms |
30836 KB |
Output is correct |
19 |
Correct |
16 ms |
30832 KB |
Output is correct |
20 |
Correct |
15 ms |
30804 KB |
Output is correct |
21 |
Correct |
14 ms |
30772 KB |
Output is correct |
22 |
Correct |
15 ms |
30832 KB |
Output is correct |
23 |
Correct |
16 ms |
30792 KB |
Output is correct |
24 |
Correct |
15 ms |
30768 KB |
Output is correct |
25 |
Correct |
18 ms |
30788 KB |
Output is correct |
26 |
Correct |
15 ms |
30784 KB |
Output is correct |
27 |
Correct |
14 ms |
30804 KB |
Output is correct |
28 |
Correct |
15 ms |
30748 KB |
Output is correct |
29 |
Correct |
16 ms |
30784 KB |
Output is correct |
30 |
Correct |
16 ms |
30836 KB |
Output is correct |
31 |
Correct |
15 ms |
30892 KB |
Output is correct |
32 |
Correct |
16 ms |
30932 KB |
Output is correct |
33 |
Correct |
16 ms |
30924 KB |
Output is correct |
34 |
Correct |
16 ms |
31060 KB |
Output is correct |
35 |
Correct |
18 ms |
31096 KB |
Output is correct |
36 |
Correct |
21 ms |
31096 KB |
Output is correct |
37 |
Correct |
23 ms |
31104 KB |
Output is correct |
38 |
Correct |
19 ms |
31184 KB |
Output is correct |
39 |
Correct |
20 ms |
31112 KB |
Output is correct |
40 |
Correct |
20 ms |
31752 KB |
Output is correct |
41 |
Correct |
20 ms |
31820 KB |
Output is correct |
42 |
Correct |
19 ms |
30996 KB |
Output is correct |
43 |
Correct |
19 ms |
31564 KB |
Output is correct |
44 |
Correct |
19 ms |
31480 KB |
Output is correct |
45 |
Correct |
20 ms |
31384 KB |
Output is correct |
46 |
Correct |
21 ms |
31296 KB |
Output is correct |
47 |
Correct |
18 ms |
31336 KB |
Output is correct |
48 |
Correct |
19 ms |
31296 KB |
Output is correct |
49 |
Correct |
19 ms |
31316 KB |
Output is correct |
50 |
Correct |
18 ms |
31176 KB |
Output is correct |
51 |
Correct |
20 ms |
31180 KB |
Output is correct |
52 |
Correct |
19 ms |
31220 KB |
Output is correct |
53 |
Correct |
21 ms |
31276 KB |
Output is correct |
54 |
Correct |
20 ms |
31316 KB |
Output is correct |
55 |
Correct |
27 ms |
31824 KB |
Output is correct |
56 |
Correct |
69 ms |
34740 KB |
Output is correct |
57 |
Correct |
112 ms |
37428 KB |
Output is correct |
58 |
Correct |
133 ms |
39296 KB |
Output is correct |
59 |
Correct |
181 ms |
42252 KB |
Output is correct |
60 |
Correct |
234 ms |
45052 KB |
Output is correct |
61 |
Correct |
281 ms |
47276 KB |
Output is correct |
62 |
Correct |
294 ms |
48812 KB |
Output is correct |
63 |
Correct |
361 ms |
52232 KB |
Output is correct |
64 |
Correct |
391 ms |
53480 KB |
Output is correct |
65 |
Correct |
255 ms |
92004 KB |
Output is correct |
66 |
Correct |
308 ms |
92080 KB |
Output is correct |
67 |
Correct |
251 ms |
45264 KB |
Output is correct |
68 |
Correct |
309 ms |
76624 KB |
Output is correct |
69 |
Correct |
338 ms |
73312 KB |
Output is correct |
70 |
Correct |
348 ms |
73288 KB |
Output is correct |
71 |
Correct |
319 ms |
68584 KB |
Output is correct |
72 |
Correct |
320 ms |
68536 KB |
Output is correct |
73 |
Correct |
321 ms |
65028 KB |
Output is correct |
74 |
Correct |
314 ms |
65192 KB |
Output is correct |
75 |
Correct |
291 ms |
63820 KB |
Output is correct |
76 |
Correct |
315 ms |
63888 KB |
Output is correct |
77 |
Correct |
299 ms |
63016 KB |
Output is correct |
78 |
Correct |
299 ms |
62300 KB |
Output is correct |
79 |
Correct |
257 ms |
64240 KB |
Output is correct |