#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)
#define lb(x,y) lower_bound(all(x),y)-begin(x)
mt19937 rng;
void solve() {
int N, M; cin >> N >> M;
vector<vector<int>> adj(N);
vector<int> len(N + M, 0);
for (int i = 1; i < N + M; i++) {
int u; cin >> u; u--;
adj[u].pb(i);
cin >> len[i];
}
vector<vector<int>> x(N + M);
vector<int> y(N + M);
for (int u = N + M - 1; u >= 0; u--) {
if (u >= N) {
x[u].pb(0);
x[u].pb(0);
y[u] = 0;
} else {
vector<int> srt;
for (int v : adj[u]) {
for (int n : x[v]) srt.pb(n);
}
sort(all(srt));
x[u].pb(srt[sz(srt) / 2 - 1]);
x[u].pb(srt[sz(srt) / 2]);
y[u] = 0;
for (int v : adj[u]) {
if (x[u][0] <= x[v][0]) {
y[u] += x[v][0] - x[u][0] + y[v];
} else if (x[u][0] <= x[v][1]) {
y[u] += y[v];
} else {
y[u] += x[u][0] - x[v][1] + y[v];
}
}
}
x[u][0] += len[u];
x[u][1] += len[u];
}
cout << y[0] << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
rng = mt19937(chrono::steady_clock::now().time_since_epoch().count());
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |