#include <bits/stdc++.h>
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
const int N = 3e5 + 5;
int n, m, c[N], ptr[N];
vector<int> g[N];
struct slope {
int a, b;
priority_queue<int> pq;
void move(int c) {
//After pq.top() is slope 1.
int curTop = pq.top();
pq.pop();
int nxtTop = -1;
if (pq.size()) {
nxtTop = pq.top();
pq.pop();
}
pq.push(curTop + c);
if (nxtTop != -1) pq.push(nxtTop + c);
b -= c;
}
int pqSize() {
return pq.size();
}
int getTop() {
if (pq.empty()) return -1;
int ans = pq.top();
pq.pop();
return ans;
}
void pushIn(int val) {
pq.push(val);
}
} dp[N];
void merge(slope &s1, slope &s2) {
s1.a += s2.a;
s1.b += s2.b;
while (1) {
int val = s2.getTop();
if (val == -1) break;
s1.pushIn(val);
}
//Pop off the top while the slope is still > 1
while (s1.a > 1) {
int x = s1.pq.top(); s1.pq.pop();
s1.a--;
s1.b += x;
}
}
void dfs(int u) {
if (g[u].size() == 0) {
// cout << "uhh u = " << u + 1 << "\n";
dp[u].pq.push(0);
dp[u].pq.push(0);
dp[u].a = 1, dp[u].b = 0;
ptr[u] = u;
return;
}
fa (v, g[u]) dfs(v);
// cout << "U = " << u + 1 << "\n";
fa (v, g[u]) dp[ptr[v]].move(c[v]);
// cout << "all moves performed\n";
int bigChild = -1;
fa (v, g[u]) {
if (bigChild == -1 || dp[ptr[v]].pqSize() > dp[ptr[bigChild]].pqSize()) bigChild = v;
}
// cout << "bigChild found = " << bigChild << "\n";
ptr[u] = ptr[bigChild];
fa (v, g[u]) if (v != bigChild) {
merge(dp[ptr[u]], dp[ptr[v]]);
}
}
signed main() {
#ifdef BLU
in("blu.inp");
#endif
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// so uhh get dp(v) and move it to the right by C(v) units, merge it with |C(v) - X|
cin >> n >> m;
fw (i, 1, n + m) {
int p;
cin >> p >> c[i];
p--;
g[p].pb(i);
}
// a function actually only needs slope -1 -> 0 and 0 -> 1 moved when moving to the right
// merging functions are just unioning their pqs and summing up a and b.
dfs(0);
slope s = dp[ptr[0]];
int x = s.pq.top(), a = s.a, b = s.b;
cout << x * a + b;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19072 KB |
Output is correct |
2 |
Incorrect |
11 ms |
19072 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19072 KB |
Output is correct |
2 |
Correct |
11 ms |
19072 KB |
Output is correct |
3 |
Incorrect |
13 ms |
19072 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19072 KB |
Output is correct |
2 |
Incorrect |
11 ms |
19072 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19072 KB |
Output is correct |
2 |
Incorrect |
11 ms |
19072 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |