#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 2e14;
const int MAXN = 5005;
int n, m;
vector<pair<int, int> > adj[MAXN];
ll val[MAXN], dp[MAXN][MAXN];
vector<ll> walk;
void precalc(int node){
for(pair<int, int> x : adj[node]){
val[x.first] = val[node] + x.second;
precalc(x.first);
}
walk.push_back(val[node]);
walk.push_back(val[node] - 1);
walk.push_back(val[node] + 1);
}
void dfs(int node){
if(node >= n){
for(int i = 0; i < (int)walk.size(); ++i){
if(val[node] == walk[i]) dp[node][i] = 0;
else dp[node][i] = inf;
}
}
for(pair<int, int> x : adj[node]){
dfs(x.first);
multiset<ll> lo, hi;
vector<ll> uk ((int)walk.size());
for(int i = 0; i < (int)walk.size(); ++i){
lo.insert(dp[x.first][i] - walk[i]);
uk[i] = walk[i] + *lo.begin();
}
int p = (int)walk.size() - 1;
for(int i = (int)walk.size() - 1; i >= 0; --i){
hi.insert(dp[x.first][i] + walk[i]);
while(p > i && x.second + walk[i] - walk[p] < 0){
assert(hi.find(dp[x.first][p] + walk[p]) != hi.end());
hi.erase(hi.find(dp[x.first][p] + walk[p]));
--p;
}
uk[i] = min(uk[i], *hi.begin() - walk[i]);
}
for(int i = 0; i < (int)walk.size(); ++i){
dp[node][i] += uk[i];
}
}
}
int main(){
cin >> n >> m;
for(int i = 1; i < n + m; ++i){
int p, c;
cin >> p >> c, --p;
adj[p].push_back({i, c});
}
precalc(0);
sort(walk.begin(), walk.end());
walk.erase(unique(walk.begin(), walk.end()), walk.end());
dfs(0);
ll ans = dp[0][0];
for(int i = 1; i < (int)walk.size(); ++i) ans = min(ans, dp[0][i]);
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
372 KB |
Output is correct |
3 |
Correct |
5 ms |
980 KB |
Output is correct |
4 |
Correct |
4 ms |
980 KB |
Output is correct |
5 |
Correct |
4 ms |
852 KB |
Output is correct |
6 |
Correct |
4 ms |
980 KB |
Output is correct |
7 |
Correct |
5 ms |
980 KB |
Output is correct |
8 |
Correct |
6 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
852 KB |
Output is correct |
5 |
Correct |
2 ms |
980 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
1108 KB |
Output is correct |
8 |
Correct |
3 ms |
1236 KB |
Output is correct |
9 |
Correct |
3 ms |
1364 KB |
Output is correct |
10 |
Correct |
3 ms |
1364 KB |
Output is correct |
11 |
Correct |
6 ms |
1644 KB |
Output is correct |
12 |
Correct |
6 ms |
1748 KB |
Output is correct |
13 |
Correct |
7 ms |
1748 KB |
Output is correct |
14 |
Correct |
10 ms |
2388 KB |
Output is correct |
15 |
Correct |
7 ms |
1968 KB |
Output is correct |
16 |
Correct |
5 ms |
1876 KB |
Output is correct |
17 |
Correct |
7 ms |
2004 KB |
Output is correct |
18 |
Correct |
8 ms |
1932 KB |
Output is correct |
19 |
Correct |
8 ms |
2036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
372 KB |
Output is correct |
3 |
Correct |
5 ms |
980 KB |
Output is correct |
4 |
Correct |
4 ms |
980 KB |
Output is correct |
5 |
Correct |
4 ms |
852 KB |
Output is correct |
6 |
Correct |
4 ms |
980 KB |
Output is correct |
7 |
Correct |
5 ms |
980 KB |
Output is correct |
8 |
Correct |
6 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
2 ms |
852 KB |
Output is correct |
15 |
Correct |
2 ms |
980 KB |
Output is correct |
16 |
Correct |
2 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
1108 KB |
Output is correct |
18 |
Correct |
3 ms |
1236 KB |
Output is correct |
19 |
Correct |
3 ms |
1364 KB |
Output is correct |
20 |
Correct |
3 ms |
1364 KB |
Output is correct |
21 |
Correct |
6 ms |
1644 KB |
Output is correct |
22 |
Correct |
6 ms |
1748 KB |
Output is correct |
23 |
Correct |
7 ms |
1748 KB |
Output is correct |
24 |
Correct |
10 ms |
2388 KB |
Output is correct |
25 |
Correct |
7 ms |
1968 KB |
Output is correct |
26 |
Correct |
5 ms |
1876 KB |
Output is correct |
27 |
Correct |
7 ms |
2004 KB |
Output is correct |
28 |
Correct |
8 ms |
1932 KB |
Output is correct |
29 |
Correct |
8 ms |
2036 KB |
Output is correct |
30 |
Incorrect |
104 ms |
7328 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
372 KB |
Output is correct |
3 |
Correct |
5 ms |
980 KB |
Output is correct |
4 |
Correct |
4 ms |
980 KB |
Output is correct |
5 |
Correct |
4 ms |
852 KB |
Output is correct |
6 |
Correct |
4 ms |
980 KB |
Output is correct |
7 |
Correct |
5 ms |
980 KB |
Output is correct |
8 |
Correct |
6 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
2 ms |
852 KB |
Output is correct |
15 |
Correct |
2 ms |
980 KB |
Output is correct |
16 |
Correct |
2 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
1108 KB |
Output is correct |
18 |
Correct |
3 ms |
1236 KB |
Output is correct |
19 |
Correct |
3 ms |
1364 KB |
Output is correct |
20 |
Correct |
3 ms |
1364 KB |
Output is correct |
21 |
Correct |
6 ms |
1644 KB |
Output is correct |
22 |
Correct |
6 ms |
1748 KB |
Output is correct |
23 |
Correct |
7 ms |
1748 KB |
Output is correct |
24 |
Correct |
10 ms |
2388 KB |
Output is correct |
25 |
Correct |
7 ms |
1968 KB |
Output is correct |
26 |
Correct |
5 ms |
1876 KB |
Output is correct |
27 |
Correct |
7 ms |
2004 KB |
Output is correct |
28 |
Correct |
8 ms |
1932 KB |
Output is correct |
29 |
Correct |
8 ms |
2036 KB |
Output is correct |
30 |
Incorrect |
104 ms |
7328 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |