#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){
N=M;
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
9 ms |
544 KB |
Output is correct |
5 |
Correct |
3 ms |
544 KB |
Output is correct |
6 |
Correct |
3 ms |
544 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
3 ms |
620 KB |
Output is correct |
9 |
Correct |
2 ms |
620 KB |
Output is correct |
10 |
Correct |
3 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
692 KB |
Output is correct |
2 |
Correct |
11 ms |
748 KB |
Output is correct |
3 |
Correct |
16 ms |
876 KB |
Output is correct |
4 |
Correct |
28 ms |
1132 KB |
Output is correct |
5 |
Correct |
39 ms |
1260 KB |
Output is correct |
6 |
Correct |
35 ms |
1260 KB |
Output is correct |
7 |
Correct |
39 ms |
1388 KB |
Output is correct |
8 |
Correct |
46 ms |
1516 KB |
Output is correct |
9 |
Correct |
50 ms |
1644 KB |
Output is correct |
10 |
Correct |
51 ms |
1772 KB |
Output is correct |
11 |
Correct |
57 ms |
1916 KB |
Output is correct |
12 |
Correct |
85 ms |
2044 KB |
Output is correct |
13 |
Correct |
70 ms |
2044 KB |
Output is correct |
14 |
Correct |
74 ms |
2196 KB |
Output is correct |
15 |
Correct |
71 ms |
2300 KB |
Output is correct |
16 |
Correct |
71 ms |
2412 KB |
Output is correct |
17 |
Correct |
71 ms |
2412 KB |
Output is correct |
18 |
Correct |
82 ms |
2412 KB |
Output is correct |
19 |
Correct |
82 ms |
2412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
9 ms |
544 KB |
Output is correct |
5 |
Correct |
3 ms |
544 KB |
Output is correct |
6 |
Correct |
3 ms |
544 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
3 ms |
620 KB |
Output is correct |
9 |
Correct |
2 ms |
620 KB |
Output is correct |
10 |
Correct |
3 ms |
620 KB |
Output is correct |
11 |
Correct |
6 ms |
692 KB |
Output is correct |
12 |
Correct |
11 ms |
748 KB |
Output is correct |
13 |
Correct |
16 ms |
876 KB |
Output is correct |
14 |
Correct |
28 ms |
1132 KB |
Output is correct |
15 |
Correct |
39 ms |
1260 KB |
Output is correct |
16 |
Correct |
35 ms |
1260 KB |
Output is correct |
17 |
Correct |
39 ms |
1388 KB |
Output is correct |
18 |
Correct |
46 ms |
1516 KB |
Output is correct |
19 |
Correct |
50 ms |
1644 KB |
Output is correct |
20 |
Correct |
51 ms |
1772 KB |
Output is correct |
21 |
Correct |
57 ms |
1916 KB |
Output is correct |
22 |
Correct |
85 ms |
2044 KB |
Output is correct |
23 |
Correct |
70 ms |
2044 KB |
Output is correct |
24 |
Correct |
74 ms |
2196 KB |
Output is correct |
25 |
Correct |
71 ms |
2300 KB |
Output is correct |
26 |
Correct |
71 ms |
2412 KB |
Output is correct |
27 |
Correct |
71 ms |
2412 KB |
Output is correct |
28 |
Correct |
82 ms |
2412 KB |
Output is correct |
29 |
Correct |
82 ms |
2412 KB |
Output is correct |
30 |
Runtime error |
5 ms |
2412 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
9 ms |
544 KB |
Output is correct |
5 |
Correct |
3 ms |
544 KB |
Output is correct |
6 |
Correct |
3 ms |
544 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
3 ms |
620 KB |
Output is correct |
9 |
Correct |
2 ms |
620 KB |
Output is correct |
10 |
Correct |
3 ms |
620 KB |
Output is correct |
11 |
Correct |
6 ms |
692 KB |
Output is correct |
12 |
Correct |
11 ms |
748 KB |
Output is correct |
13 |
Correct |
16 ms |
876 KB |
Output is correct |
14 |
Correct |
28 ms |
1132 KB |
Output is correct |
15 |
Correct |
39 ms |
1260 KB |
Output is correct |
16 |
Correct |
35 ms |
1260 KB |
Output is correct |
17 |
Correct |
39 ms |
1388 KB |
Output is correct |
18 |
Correct |
46 ms |
1516 KB |
Output is correct |
19 |
Correct |
50 ms |
1644 KB |
Output is correct |
20 |
Correct |
51 ms |
1772 KB |
Output is correct |
21 |
Correct |
57 ms |
1916 KB |
Output is correct |
22 |
Correct |
85 ms |
2044 KB |
Output is correct |
23 |
Correct |
70 ms |
2044 KB |
Output is correct |
24 |
Correct |
74 ms |
2196 KB |
Output is correct |
25 |
Correct |
71 ms |
2300 KB |
Output is correct |
26 |
Correct |
71 ms |
2412 KB |
Output is correct |
27 |
Correct |
71 ms |
2412 KB |
Output is correct |
28 |
Correct |
82 ms |
2412 KB |
Output is correct |
29 |
Correct |
82 ms |
2412 KB |
Output is correct |
30 |
Runtime error |
5 ms |
2412 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
31 |
Halted |
0 ms |
0 KB |
- |