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>
using namespace std;
#define ull unsigned long long
#define pb push_back
#define ld long double
#define ll long long
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N = 3e5 + 10;
vector<int> g[N];
multiset<int> st[N];
ll k[N], b[N];
int c[N];
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i < n + m; i++) {
int p;
cin >> p >> c[i];
p--;
g[p].pb(i);
}
for (int i = n; i < n + m; i++) {
k[i] = 1;
b[i] = -c[i];
st[i].insert(c[i]);
st[i].insert(c[i]);
}
for (int i = n - 1; i >= 0; i--) {
for (auto to : g[i]) {
k[i] += k[to];
b[i] += b[to];
if (st[to].size() > st[i].size()) {
swap(st[i], st[to]);
}
for (auto x : st[to]) {
st[i].insert(x);
}
}
while (k[i] > 1) {
k[i]--;
b[i] += *st[i].rbegin();
st[i].erase(st[i].find(*st[i].rbegin()));
}
if (c[i] > 0) {
int slope1 = *st[i].rbegin();
st[i].erase(st[i].find(slope1));
int slope0 = *st[i].rbegin();
st[i].erase(st[i].find(slope0));
st[i].insert(slope0 + c[i]);
st[i].insert(slope1 + c[i]);
b[i] -= c[i];
}
}
while (k[0] > 0) {
k[0]--;
b[0] += *st[0].rbegin();
st[0].erase(st[0].find(*st[0].rbegin()));
}
cout << b[0] << endl;
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... |