#pragma GCC optimize("Ofast")
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pll;
ll x,y;
ll INF=1e18;
ll N,M,K=600;
ll DP[305][700];
vector<pll> grafo[305];
ll ans;
ll V[1000006];
void DFS(ll k, ll p){
if(k>N){
for(int i=1;i<K;i++)
DP[k][i]=INF;
return;
}
for(pll f:grafo[k])
if(f.first!=p)
DFS(f.first,k);
for(int i=0;i<K;i++)
for(pll f:grafo[k])
if(f.first!=p){
ll res=INF;
for(int j=0;j<=i;j++)
res=min(res,DP[f.first][j]+abs(f.second-(i-j)));
DP[k][i]+=res;
}
}
int main(){
cin>>N>>M;
if(N==1){
for(int i=0;i<N;i++)
cin>>V[i]>>V[i];
sort(V,V+N);
for(int i=0;i<N;i++)
ans+=abs(V[i]-V[N/2]);
}
else{
for(int i=2;i<=N+M;i++){
cin>>x>>y;
grafo[i].push_back({x,y});
grafo[x].push_back({i,y});
}
DFS(1,0);
ans=INF;
for(int i=0;i<K;i++)
ans=min(ans,DP[1][i]);
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
436 KB |
Output is correct |
2 |
Correct |
12 ms |
564 KB |
Output is correct |
3 |
Correct |
16 ms |
724 KB |
Output is correct |
4 |
Correct |
28 ms |
1108 KB |
Output is correct |
5 |
Correct |
31 ms |
1132 KB |
Output is correct |
6 |
Correct |
32 ms |
1232 KB |
Output is correct |
7 |
Correct |
42 ms |
1420 KB |
Output is correct |
8 |
Correct |
47 ms |
1592 KB |
Output is correct |
9 |
Correct |
54 ms |
1624 KB |
Output is correct |
10 |
Correct |
84 ms |
1764 KB |
Output is correct |
11 |
Correct |
69 ms |
2020 KB |
Output is correct |
12 |
Correct |
73 ms |
2020 KB |
Output is correct |
13 |
Correct |
71 ms |
2148 KB |
Output is correct |
14 |
Correct |
89 ms |
2148 KB |
Output is correct |
15 |
Correct |
79 ms |
2404 KB |
Output is correct |
16 |
Correct |
85 ms |
2404 KB |
Output is correct |
17 |
Correct |
71 ms |
2404 KB |
Output is correct |
18 |
Correct |
73 ms |
2404 KB |
Output is correct |
19 |
Correct |
77 ms |
2404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |