#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<ll> 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 |
1 |
Correct |
10 ms |
21460 KB |
Output is correct |
2 |
Correct |
12 ms |
21400 KB |
Output is correct |
3 |
Correct |
10 ms |
21460 KB |
Output is correct |
4 |
Correct |
10 ms |
21460 KB |
Output is correct |
5 |
Correct |
11 ms |
21460 KB |
Output is correct |
6 |
Correct |
10 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21388 KB |
Output is correct |
8 |
Correct |
11 ms |
21460 KB |
Output is correct |
9 |
Correct |
10 ms |
21436 KB |
Output is correct |
10 |
Correct |
10 ms |
21460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
21364 KB |
Output is correct |
2 |
Correct |
11 ms |
21460 KB |
Output is correct |
3 |
Correct |
10 ms |
21484 KB |
Output is correct |
4 |
Correct |
13 ms |
21460 KB |
Output is correct |
5 |
Correct |
11 ms |
21460 KB |
Output is correct |
6 |
Correct |
10 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21460 KB |
Output is correct |
8 |
Correct |
10 ms |
21460 KB |
Output is correct |
9 |
Correct |
10 ms |
21460 KB |
Output is correct |
10 |
Correct |
11 ms |
21460 KB |
Output is correct |
11 |
Correct |
10 ms |
21460 KB |
Output is correct |
12 |
Correct |
10 ms |
21512 KB |
Output is correct |
13 |
Correct |
10 ms |
21488 KB |
Output is correct |
14 |
Correct |
10 ms |
21380 KB |
Output is correct |
15 |
Correct |
11 ms |
21460 KB |
Output is correct |
16 |
Correct |
10 ms |
21464 KB |
Output is correct |
17 |
Correct |
10 ms |
21460 KB |
Output is correct |
18 |
Correct |
10 ms |
21420 KB |
Output is correct |
19 |
Correct |
10 ms |
21460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
21460 KB |
Output is correct |
2 |
Correct |
12 ms |
21400 KB |
Output is correct |
3 |
Correct |
10 ms |
21460 KB |
Output is correct |
4 |
Correct |
10 ms |
21460 KB |
Output is correct |
5 |
Correct |
11 ms |
21460 KB |
Output is correct |
6 |
Correct |
10 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21388 KB |
Output is correct |
8 |
Correct |
11 ms |
21460 KB |
Output is correct |
9 |
Correct |
10 ms |
21436 KB |
Output is correct |
10 |
Correct |
10 ms |
21460 KB |
Output is correct |
11 |
Correct |
10 ms |
21364 KB |
Output is correct |
12 |
Correct |
11 ms |
21460 KB |
Output is correct |
13 |
Correct |
10 ms |
21484 KB |
Output is correct |
14 |
Correct |
13 ms |
21460 KB |
Output is correct |
15 |
Correct |
11 ms |
21460 KB |
Output is correct |
16 |
Correct |
10 ms |
21460 KB |
Output is correct |
17 |
Correct |
11 ms |
21460 KB |
Output is correct |
18 |
Correct |
10 ms |
21460 KB |
Output is correct |
19 |
Correct |
10 ms |
21460 KB |
Output is correct |
20 |
Correct |
11 ms |
21460 KB |
Output is correct |
21 |
Correct |
10 ms |
21460 KB |
Output is correct |
22 |
Correct |
10 ms |
21512 KB |
Output is correct |
23 |
Correct |
10 ms |
21488 KB |
Output is correct |
24 |
Correct |
10 ms |
21380 KB |
Output is correct |
25 |
Correct |
11 ms |
21460 KB |
Output is correct |
26 |
Correct |
10 ms |
21464 KB |
Output is correct |
27 |
Correct |
10 ms |
21460 KB |
Output is correct |
28 |
Correct |
10 ms |
21420 KB |
Output is correct |
29 |
Correct |
10 ms |
21460 KB |
Output is correct |
30 |
Incorrect |
10 ms |
21432 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
21460 KB |
Output is correct |
2 |
Correct |
12 ms |
21400 KB |
Output is correct |
3 |
Correct |
10 ms |
21460 KB |
Output is correct |
4 |
Correct |
10 ms |
21460 KB |
Output is correct |
5 |
Correct |
11 ms |
21460 KB |
Output is correct |
6 |
Correct |
10 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21388 KB |
Output is correct |
8 |
Correct |
11 ms |
21460 KB |
Output is correct |
9 |
Correct |
10 ms |
21436 KB |
Output is correct |
10 |
Correct |
10 ms |
21460 KB |
Output is correct |
11 |
Correct |
10 ms |
21364 KB |
Output is correct |
12 |
Correct |
11 ms |
21460 KB |
Output is correct |
13 |
Correct |
10 ms |
21484 KB |
Output is correct |
14 |
Correct |
13 ms |
21460 KB |
Output is correct |
15 |
Correct |
11 ms |
21460 KB |
Output is correct |
16 |
Correct |
10 ms |
21460 KB |
Output is correct |
17 |
Correct |
11 ms |
21460 KB |
Output is correct |
18 |
Correct |
10 ms |
21460 KB |
Output is correct |
19 |
Correct |
10 ms |
21460 KB |
Output is correct |
20 |
Correct |
11 ms |
21460 KB |
Output is correct |
21 |
Correct |
10 ms |
21460 KB |
Output is correct |
22 |
Correct |
10 ms |
21512 KB |
Output is correct |
23 |
Correct |
10 ms |
21488 KB |
Output is correct |
24 |
Correct |
10 ms |
21380 KB |
Output is correct |
25 |
Correct |
11 ms |
21460 KB |
Output is correct |
26 |
Correct |
10 ms |
21464 KB |
Output is correct |
27 |
Correct |
10 ms |
21460 KB |
Output is correct |
28 |
Correct |
10 ms |
21420 KB |
Output is correct |
29 |
Correct |
10 ms |
21460 KB |
Output is correct |
30 |
Incorrect |
10 ms |
21432 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |