This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define DEBUG false
using namespace std;
const int MAX_N = 3e5 + 10;
int P[MAX_N], C[MAX_N];
class SlopeTrick {
public :
long long m, c;
priority_queue <long long> *pq;
SlopeTrick() : m(0), c(0), pq(new priority_queue <long long>) {}
SlopeTrick operator+(const SlopeTrick &o) const {
SlopeTrick result;
result.m = m + o.m;
result.c = c + o.c;
if(pq->size() > o.pq->size()) {
result.pq = pq;
while(!o.pq->empty()) {
result.pq->push(o.pq->top());
o.pq->pop();
}
}
else {
result.pq = o.pq;
while(!pq->empty()) {
result.pq->push(pq->top());
pq->pop();
}
}
return result;
}
}D[MAX_N];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M;
cin >> N >> M;
for(int i = 2; i <= N + M; i++) {
cin >> P[i] >> C[i];
}
for(int i = N + 1; i <= N + M; i++) {
D[i].m = 1;
D[i].c = -C[i];
D[i].pq->push(C[i]);
D[i].pq->push(C[i]);
D[P[i]] = D[P[i]] + D[i];
}
for(int i = N; i >= 2; i--) {
while(D[i].m > 1) {
D[i].m--;
D[i].c += D[i].pq->top();
D[i].pq->pop();
}
long long slope1 = D[i].pq->top();
D[i].pq->pop();
long long slope0 = D[i].pq->top();
D[i].pq->pop();
D[i].pq->push(slope1 + C[i]);
D[i].pq->push(slope0 + C[i]);
D[i].c -= C[i];
D[P[i]] = D[P[i]] + D[i];
}
while(D[1].m > 0) {
D[1].m--;
D[1].c += D[1].pq->top();
D[1].pq->pop();
}
cout << D[1].c;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |