#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,avx2")
#pragma GCC optimize("unroll-loops,O2")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 3e2+15,mod = 1e9+7;
constexpr ll inf = 1e18+10;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
// if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k >>= 1;
}
return z;
}
vector<pll> adj[N];
ll h[N],val[N][2];
int n,m;
ll dp[N][N];
void pre(int v){
if (v > n) val[v][0] = val[v][1] = h[v];
else{
val[v][1] = -inf;
val[v][0] = inf;
}
for (pll p : adj[v]){
int u = p.X;
h[u] = h[v]+p.Y;
pre(u);
val[v][1] = max(val[v][1],val[u][1]);
val[v][0] = min(val[v][0],val[u][0]);
}
}
void dfs(int v){
for (auto [u,w] : adj[v]) dfs(u);
rep(i,0,N){
if (v > n && i){
dp[v][i] = inf;
continue;
}
dp[v][i] = 0;
for (auto [u,w] : adj[v]){
ll mi = inf;
rep(j,0,i+1){
ll cost;
if (j+w <= i) cost = i-j-w;
else cost = j+w-i;
mi = min(mi,dp[u][j]+cost);
}
dp[v][i] += mi;
}
}
}
int main(){
ios :: sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
rep(i,2,n+m+1){
int p,w;
cin >> p >> w;
adj[p].pb({i,w});
}
pre(1);
ll out = inf;
dfs(1);
rep(i,0,N) out = min(out,dp[1][i]);
cout << out;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
6 ms |
468 KB |
Output is correct |
4 |
Correct |
11 ms |
468 KB |
Output is correct |
5 |
Correct |
9 ms |
596 KB |
Output is correct |
6 |
Correct |
9 ms |
596 KB |
Output is correct |
7 |
Correct |
10 ms |
724 KB |
Output is correct |
8 |
Correct |
18 ms |
724 KB |
Output is correct |
9 |
Correct |
20 ms |
724 KB |
Output is correct |
10 |
Correct |
20 ms |
860 KB |
Output is correct |
11 |
Correct |
24 ms |
864 KB |
Output is correct |
12 |
Correct |
18 ms |
976 KB |
Output is correct |
13 |
Correct |
17 ms |
1004 KB |
Output is correct |
14 |
Correct |
21 ms |
1000 KB |
Output is correct |
15 |
Correct |
24 ms |
1076 KB |
Output is correct |
16 |
Correct |
19 ms |
1080 KB |
Output is correct |
17 |
Correct |
20 ms |
980 KB |
Output is correct |
18 |
Correct |
21 ms |
988 KB |
Output is correct |
19 |
Correct |
19 ms |
1032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |