#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define eb emplace_back
#define em emplace
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const int INF = 1 << 30;
const ll LINF = 1LL << 62;
const int MAX = 303030;
ll ans = 0;
struct node{
priority_queue <ll> L;
priority_queue <ll, vector <ll>, greater <ll> > R;
int sz = 0;
void merge(node &X) {
if(X.sz == 0) return;
sz += X.sz;
while(!X.L.empty()) {
ll temp = X.L.top();
X.L.pop();
if(temp > R.top()){
ans += temp - R.top();
L.em(R.top()); R.pop();
R.em(temp);
}
else L.em(temp);
}
while(!X.R.empty()) {
ll temp = X.R.top();
X.R.pop();
if(temp < L.top()){
ans += L.top() - temp;
R.em(L.top()); L.pop();
L.em(temp);
}
else R.em(temp);
}
}
void modify(ll val) {
ll Rt = 0, Lt = 0;
if(!R.empty()) {Rt = R.top();}
if(!L.empty()) {Lt = L.top(); L.pop();}
while(!R.empty()) R.pop(), sz--;
R.push(Rt + val);
L.push(Lt + val);
sz += 2;
}
} dp[MAX];
int p[MAX], idx[MAX];
ll c[MAX];
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n, m;
cin >> n >> m;
for(int i = 2; i <= n + m; i++) {
cin >> p[i] >> c[i];
idx[i] = i;
}
for(int i = n + m; i > 1; i--) {
dp[idx[i]].modify(c[i]);
if(dp[idx[p[i]]].sz < dp[idx[i]].sz) {
dp[idx[i]].merge(dp[idx[p[i]]]);
idx[p[i]] = idx[i];
}
else dp[idx[p[i]]].merge(dp[idx[i]]);
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
21752 KB |
Output is correct |
2 |
Correct |
21 ms |
21752 KB |
Output is correct |
3 |
Correct |
21 ms |
21752 KB |
Output is correct |
4 |
Correct |
21 ms |
21624 KB |
Output is correct |
5 |
Correct |
21 ms |
21752 KB |
Output is correct |
6 |
Correct |
21 ms |
21752 KB |
Output is correct |
7 |
Correct |
21 ms |
21752 KB |
Output is correct |
8 |
Correct |
21 ms |
21752 KB |
Output is correct |
9 |
Correct |
21 ms |
21628 KB |
Output is correct |
10 |
Correct |
21 ms |
21752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
21724 KB |
Output is correct |
2 |
Correct |
21 ms |
21624 KB |
Output is correct |
3 |
Correct |
26 ms |
21624 KB |
Output is correct |
4 |
Correct |
21 ms |
21752 KB |
Output is correct |
5 |
Correct |
22 ms |
21784 KB |
Output is correct |
6 |
Correct |
21 ms |
21752 KB |
Output is correct |
7 |
Correct |
21 ms |
21752 KB |
Output is correct |
8 |
Correct |
22 ms |
21752 KB |
Output is correct |
9 |
Correct |
21 ms |
21752 KB |
Output is correct |
10 |
Correct |
22 ms |
21724 KB |
Output is correct |
11 |
Correct |
23 ms |
21752 KB |
Output is correct |
12 |
Correct |
21 ms |
21756 KB |
Output is correct |
13 |
Correct |
22 ms |
21752 KB |
Output is correct |
14 |
Correct |
21 ms |
21752 KB |
Output is correct |
15 |
Correct |
30 ms |
21752 KB |
Output is correct |
16 |
Correct |
24 ms |
21808 KB |
Output is correct |
17 |
Correct |
23 ms |
21752 KB |
Output is correct |
18 |
Correct |
22 ms |
21752 KB |
Output is correct |
19 |
Correct |
23 ms |
21728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
21752 KB |
Output is correct |
2 |
Correct |
21 ms |
21752 KB |
Output is correct |
3 |
Correct |
21 ms |
21752 KB |
Output is correct |
4 |
Correct |
21 ms |
21624 KB |
Output is correct |
5 |
Correct |
21 ms |
21752 KB |
Output is correct |
6 |
Correct |
21 ms |
21752 KB |
Output is correct |
7 |
Correct |
21 ms |
21752 KB |
Output is correct |
8 |
Correct |
21 ms |
21752 KB |
Output is correct |
9 |
Correct |
21 ms |
21628 KB |
Output is correct |
10 |
Correct |
21 ms |
21752 KB |
Output is correct |
11 |
Correct |
21 ms |
21724 KB |
Output is correct |
12 |
Correct |
21 ms |
21624 KB |
Output is correct |
13 |
Correct |
26 ms |
21624 KB |
Output is correct |
14 |
Correct |
21 ms |
21752 KB |
Output is correct |
15 |
Correct |
22 ms |
21784 KB |
Output is correct |
16 |
Correct |
21 ms |
21752 KB |
Output is correct |
17 |
Correct |
21 ms |
21752 KB |
Output is correct |
18 |
Correct |
22 ms |
21752 KB |
Output is correct |
19 |
Correct |
21 ms |
21752 KB |
Output is correct |
20 |
Correct |
22 ms |
21724 KB |
Output is correct |
21 |
Correct |
23 ms |
21752 KB |
Output is correct |
22 |
Correct |
21 ms |
21756 KB |
Output is correct |
23 |
Correct |
22 ms |
21752 KB |
Output is correct |
24 |
Correct |
21 ms |
21752 KB |
Output is correct |
25 |
Correct |
30 ms |
21752 KB |
Output is correct |
26 |
Correct |
24 ms |
21808 KB |
Output is correct |
27 |
Correct |
23 ms |
21752 KB |
Output is correct |
28 |
Correct |
22 ms |
21752 KB |
Output is correct |
29 |
Correct |
23 ms |
21728 KB |
Output is correct |
30 |
Correct |
22 ms |
21752 KB |
Output is correct |
31 |
Correct |
23 ms |
21752 KB |
Output is correct |
32 |
Correct |
23 ms |
21752 KB |
Output is correct |
33 |
Correct |
24 ms |
21880 KB |
Output is correct |
34 |
Correct |
23 ms |
22008 KB |
Output is correct |
35 |
Correct |
24 ms |
22008 KB |
Output is correct |
36 |
Correct |
24 ms |
22008 KB |
Output is correct |
37 |
Correct |
26 ms |
22136 KB |
Output is correct |
38 |
Correct |
24 ms |
22136 KB |
Output is correct |
39 |
Correct |
26 ms |
22084 KB |
Output is correct |
40 |
Correct |
24 ms |
21880 KB |
Output is correct |
41 |
Correct |
25 ms |
21880 KB |
Output is correct |
42 |
Correct |
24 ms |
21896 KB |
Output is correct |
43 |
Correct |
24 ms |
22136 KB |
Output is correct |
44 |
Correct |
25 ms |
22008 KB |
Output is correct |
45 |
Correct |
24 ms |
22136 KB |
Output is correct |
46 |
Correct |
26 ms |
22392 KB |
Output is correct |
47 |
Correct |
26 ms |
22392 KB |
Output is correct |
48 |
Correct |
26 ms |
22264 KB |
Output is correct |
49 |
Correct |
26 ms |
22392 KB |
Output is correct |
50 |
Correct |
25 ms |
22236 KB |
Output is correct |
51 |
Correct |
24 ms |
22236 KB |
Output is correct |
52 |
Correct |
26 ms |
22264 KB |
Output is correct |
53 |
Correct |
25 ms |
22136 KB |
Output is correct |
54 |
Correct |
25 ms |
22264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
21752 KB |
Output is correct |
2 |
Correct |
21 ms |
21752 KB |
Output is correct |
3 |
Correct |
21 ms |
21752 KB |
Output is correct |
4 |
Correct |
21 ms |
21624 KB |
Output is correct |
5 |
Correct |
21 ms |
21752 KB |
Output is correct |
6 |
Correct |
21 ms |
21752 KB |
Output is correct |
7 |
Correct |
21 ms |
21752 KB |
Output is correct |
8 |
Correct |
21 ms |
21752 KB |
Output is correct |
9 |
Correct |
21 ms |
21628 KB |
Output is correct |
10 |
Correct |
21 ms |
21752 KB |
Output is correct |
11 |
Correct |
21 ms |
21724 KB |
Output is correct |
12 |
Correct |
21 ms |
21624 KB |
Output is correct |
13 |
Correct |
26 ms |
21624 KB |
Output is correct |
14 |
Correct |
21 ms |
21752 KB |
Output is correct |
15 |
Correct |
22 ms |
21784 KB |
Output is correct |
16 |
Correct |
21 ms |
21752 KB |
Output is correct |
17 |
Correct |
21 ms |
21752 KB |
Output is correct |
18 |
Correct |
22 ms |
21752 KB |
Output is correct |
19 |
Correct |
21 ms |
21752 KB |
Output is correct |
20 |
Correct |
22 ms |
21724 KB |
Output is correct |
21 |
Correct |
23 ms |
21752 KB |
Output is correct |
22 |
Correct |
21 ms |
21756 KB |
Output is correct |
23 |
Correct |
22 ms |
21752 KB |
Output is correct |
24 |
Correct |
21 ms |
21752 KB |
Output is correct |
25 |
Correct |
30 ms |
21752 KB |
Output is correct |
26 |
Correct |
24 ms |
21808 KB |
Output is correct |
27 |
Correct |
23 ms |
21752 KB |
Output is correct |
28 |
Correct |
22 ms |
21752 KB |
Output is correct |
29 |
Correct |
23 ms |
21728 KB |
Output is correct |
30 |
Correct |
22 ms |
21752 KB |
Output is correct |
31 |
Correct |
23 ms |
21752 KB |
Output is correct |
32 |
Correct |
23 ms |
21752 KB |
Output is correct |
33 |
Correct |
24 ms |
21880 KB |
Output is correct |
34 |
Correct |
23 ms |
22008 KB |
Output is correct |
35 |
Correct |
24 ms |
22008 KB |
Output is correct |
36 |
Correct |
24 ms |
22008 KB |
Output is correct |
37 |
Correct |
26 ms |
22136 KB |
Output is correct |
38 |
Correct |
24 ms |
22136 KB |
Output is correct |
39 |
Correct |
26 ms |
22084 KB |
Output is correct |
40 |
Correct |
24 ms |
21880 KB |
Output is correct |
41 |
Correct |
25 ms |
21880 KB |
Output is correct |
42 |
Correct |
24 ms |
21896 KB |
Output is correct |
43 |
Correct |
24 ms |
22136 KB |
Output is correct |
44 |
Correct |
25 ms |
22008 KB |
Output is correct |
45 |
Correct |
24 ms |
22136 KB |
Output is correct |
46 |
Correct |
26 ms |
22392 KB |
Output is correct |
47 |
Correct |
26 ms |
22392 KB |
Output is correct |
48 |
Correct |
26 ms |
22264 KB |
Output is correct |
49 |
Correct |
26 ms |
22392 KB |
Output is correct |
50 |
Correct |
25 ms |
22236 KB |
Output is correct |
51 |
Correct |
24 ms |
22236 KB |
Output is correct |
52 |
Correct |
26 ms |
22264 KB |
Output is correct |
53 |
Correct |
25 ms |
22136 KB |
Output is correct |
54 |
Correct |
25 ms |
22264 KB |
Output is correct |
55 |
Correct |
29 ms |
22648 KB |
Output is correct |
56 |
Correct |
53 ms |
25584 KB |
Output is correct |
57 |
Correct |
78 ms |
28156 KB |
Output is correct |
58 |
Correct |
95 ms |
30016 KB |
Output is correct |
59 |
Correct |
132 ms |
32884 KB |
Output is correct |
60 |
Correct |
152 ms |
35416 KB |
Output is correct |
61 |
Correct |
176 ms |
37592 KB |
Output is correct |
62 |
Correct |
211 ms |
39276 KB |
Output is correct |
63 |
Correct |
237 ms |
42468 KB |
Output is correct |
64 |
Correct |
242 ms |
43248 KB |
Output is correct |
65 |
Correct |
145 ms |
31336 KB |
Output is correct |
66 |
Correct |
113 ms |
31280 KB |
Output is correct |
67 |
Correct |
121 ms |
31372 KB |
Output is correct |
68 |
Correct |
169 ms |
42736 KB |
Output is correct |
69 |
Correct |
207 ms |
42992 KB |
Output is correct |
70 |
Correct |
209 ms |
42992 KB |
Output is correct |
71 |
Correct |
288 ms |
62592 KB |
Output is correct |
72 |
Correct |
278 ms |
62692 KB |
Output is correct |
73 |
Correct |
246 ms |
59640 KB |
Output is correct |
74 |
Correct |
266 ms |
60276 KB |
Output is correct |
75 |
Correct |
261 ms |
58904 KB |
Output is correct |
76 |
Correct |
262 ms |
58960 KB |
Output is correct |
77 |
Correct |
276 ms |
57640 KB |
Output is correct |
78 |
Correct |
272 ms |
57440 KB |
Output is correct |
79 |
Correct |
208 ms |
53824 KB |
Output is correct |