#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define int long long
int inf=2e18;
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()) {
lr.push(k);
rr.push(k);
lr.push(0);
rr.push(inf);
}else {
int c=rr.top();
rr.pop();
rr.move(k);
rr.push(c);
rr.move(k);
c=lr.top();
lr.pop();
lr.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.lr.top();
b.lr.push(b.rr.top());
b.rr.pop();
b.val+=i-b.lr.top();
}
}
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.rr.push(b.lr.top());
b.lr.pop();
b.val+=b.rr.top()-i;
}
}
}
const int N=300010;
vector<pair<int,int>> g[N];
node res[N];
void dfs(int at) {
// res[at].add(0);
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 |
16 ms |
30804 KB |
Output is correct |
2 |
Correct |
16 ms |
30744 KB |
Output is correct |
3 |
Correct |
18 ms |
30828 KB |
Output is correct |
4 |
Correct |
16 ms |
30828 KB |
Output is correct |
5 |
Correct |
16 ms |
30804 KB |
Output is correct |
6 |
Correct |
15 ms |
30824 KB |
Output is correct |
7 |
Correct |
15 ms |
30820 KB |
Output is correct |
8 |
Correct |
15 ms |
30804 KB |
Output is correct |
9 |
Correct |
15 ms |
30792 KB |
Output is correct |
10 |
Correct |
15 ms |
30952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
30836 KB |
Output is correct |
2 |
Correct |
14 ms |
30804 KB |
Output is correct |
3 |
Correct |
16 ms |
30820 KB |
Output is correct |
4 |
Correct |
15 ms |
30796 KB |
Output is correct |
5 |
Correct |
15 ms |
30828 KB |
Output is correct |
6 |
Incorrect |
15 ms |
30824 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
30804 KB |
Output is correct |
2 |
Correct |
16 ms |
30744 KB |
Output is correct |
3 |
Correct |
18 ms |
30828 KB |
Output is correct |
4 |
Correct |
16 ms |
30828 KB |
Output is correct |
5 |
Correct |
16 ms |
30804 KB |
Output is correct |
6 |
Correct |
15 ms |
30824 KB |
Output is correct |
7 |
Correct |
15 ms |
30820 KB |
Output is correct |
8 |
Correct |
15 ms |
30804 KB |
Output is correct |
9 |
Correct |
15 ms |
30792 KB |
Output is correct |
10 |
Correct |
15 ms |
30952 KB |
Output is correct |
11 |
Correct |
15 ms |
30836 KB |
Output is correct |
12 |
Correct |
14 ms |
30804 KB |
Output is correct |
13 |
Correct |
16 ms |
30820 KB |
Output is correct |
14 |
Correct |
15 ms |
30796 KB |
Output is correct |
15 |
Correct |
15 ms |
30828 KB |
Output is correct |
16 |
Incorrect |
15 ms |
30824 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
30804 KB |
Output is correct |
2 |
Correct |
16 ms |
30744 KB |
Output is correct |
3 |
Correct |
18 ms |
30828 KB |
Output is correct |
4 |
Correct |
16 ms |
30828 KB |
Output is correct |
5 |
Correct |
16 ms |
30804 KB |
Output is correct |
6 |
Correct |
15 ms |
30824 KB |
Output is correct |
7 |
Correct |
15 ms |
30820 KB |
Output is correct |
8 |
Correct |
15 ms |
30804 KB |
Output is correct |
9 |
Correct |
15 ms |
30792 KB |
Output is correct |
10 |
Correct |
15 ms |
30952 KB |
Output is correct |
11 |
Correct |
15 ms |
30836 KB |
Output is correct |
12 |
Correct |
14 ms |
30804 KB |
Output is correct |
13 |
Correct |
16 ms |
30820 KB |
Output is correct |
14 |
Correct |
15 ms |
30796 KB |
Output is correct |
15 |
Correct |
15 ms |
30828 KB |
Output is correct |
16 |
Incorrect |
15 ms |
30824 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |