#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
struct dpvl {
ll l;
ll r;
ll mn;
dpvl() {}
dpvl(ll l_, ll r_, ll mn_): l(l_), r(r_), mn(mn_) {}
ll operator()(ll x) {
if (l <= x && x <= r)
return mn;
return min(abs(l - x), abs(r - x)) + mn;
}
};
const int MAXN = 300003;
const ll INF = 1.77013e9;
vector<pair<int, ll>> edg[MAXN];
pair<int, ll> root[MAXN];
ll lower_bound_dir(vector<dpvl>& v, ll x) {
ll lb = -INF, ub = INF;
while (ub - lb > 1) {
ll mb = (lb + ub) / 2;
ll ans = 0;
for (auto d : v) {
ans += d(mb + 1) - d(mb);
}
if (ans < x)
lb = mb;
else
ub = mb;
}
return ub;
}
dpvl dfs(int v, int n) {
if (v >= n) {
return {root[v].second, root[v].second, 0};
}
vector<dpvl> ch;
for (auto [u, d] : edg[v]) {
ch.push_back(dfs(u, n));
}
ll l = lower_bound_dir(ch, 0);
ll r = lower_bound_dir(ch, 1);
ll mn = 0;
for (auto d : ch)
mn += d(l);
return {l + root[v].second, r + root[v].second, mn};
}
signed main() {
int n, m;
cin >> n >> m;
ll addans = 0;
for (int i = 1; i < n + m; ++i) {
int u, d;
cin >> u >> d;
--u;
if (u >= n) {
addans += d;
} else {
edg[u].push_back({i, d});
root[i] = {u, d};
}
}
cout << dfs(0, n).mn << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7324 KB |
Output is correct |
3 |
Correct |
4 ms |
7344 KB |
Output is correct |
4 |
Correct |
4 ms |
7344 KB |
Output is correct |
5 |
Correct |
4 ms |
7340 KB |
Output is correct |
6 |
Correct |
4 ms |
7340 KB |
Output is correct |
7 |
Correct |
4 ms |
7244 KB |
Output is correct |
8 |
Correct |
4 ms |
7348 KB |
Output is correct |
9 |
Correct |
4 ms |
7316 KB |
Output is correct |
10 |
Correct |
4 ms |
7336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
4 ms |
7244 KB |
Output is correct |
3 |
Incorrect |
4 ms |
7340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7324 KB |
Output is correct |
3 |
Correct |
4 ms |
7344 KB |
Output is correct |
4 |
Correct |
4 ms |
7344 KB |
Output is correct |
5 |
Correct |
4 ms |
7340 KB |
Output is correct |
6 |
Correct |
4 ms |
7340 KB |
Output is correct |
7 |
Correct |
4 ms |
7244 KB |
Output is correct |
8 |
Correct |
4 ms |
7348 KB |
Output is correct |
9 |
Correct |
4 ms |
7316 KB |
Output is correct |
10 |
Correct |
4 ms |
7336 KB |
Output is correct |
11 |
Correct |
5 ms |
7244 KB |
Output is correct |
12 |
Correct |
4 ms |
7244 KB |
Output is correct |
13 |
Incorrect |
4 ms |
7340 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7372 KB |
Output is correct |
2 |
Correct |
4 ms |
7324 KB |
Output is correct |
3 |
Correct |
4 ms |
7344 KB |
Output is correct |
4 |
Correct |
4 ms |
7344 KB |
Output is correct |
5 |
Correct |
4 ms |
7340 KB |
Output is correct |
6 |
Correct |
4 ms |
7340 KB |
Output is correct |
7 |
Correct |
4 ms |
7244 KB |
Output is correct |
8 |
Correct |
4 ms |
7348 KB |
Output is correct |
9 |
Correct |
4 ms |
7316 KB |
Output is correct |
10 |
Correct |
4 ms |
7336 KB |
Output is correct |
11 |
Correct |
5 ms |
7244 KB |
Output is correct |
12 |
Correct |
4 ms |
7244 KB |
Output is correct |
13 |
Incorrect |
4 ms |
7340 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |