#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define all(x) x.begin() , x.end()
#define sze(x) (ll)(x.size())
typedef long long ll;
typedef pair<ll , ll> pll;
const ll maxn = 312 , md = 1e9 + 7 , inf = 2e16;
ll n , m;
vector<ll> v;
void sub1(){
for(ll i = 0 ; i < m ; i++){
ll w;
cin>>w>>w;
v.push_back(w);
}
sort(all(v));
ll f = v[m / 2] , ans = 0;
for(ll i = 0 ; i < m ; i++){
ans += abs(v[i] - f);
}
cout<<ans<<'\n';
return;
}
vector<pll> adj[maxn];
ll dp[maxn][maxn];
void dDFS(ll r){
if(r >= n){
dp[r][0] = 0;
return;
}
for(auto p : adj[r]){
ll i = p.first;
dDFS(i);
}
for(ll j = 0 ; j <= 300 ; j++){
ll res = 0;
for(auto p : adj[r]){
ll i = p.first , w = p.second , h = inf;
for(ll k = 0 ; k <= j ; k++){
ll t = j - k;
h = min(h , dp[i][k] + abs(t - w));
}
res += h;
}
dp[r][j] = res;
}
return;
}
void sub2(){
memset(dp , 63 , sizeof(dp));
for(ll i = 1 ; i < n + m ; i++){
ll p , w;
cin>>p>>w; p--;
adj[p].push_back({i , w});
}
dDFS(0);
ll ans = inf;
for(ll j = 0 ; j <= 300 ; j++){
ans = min(ans , dp[0][j]);
}
cout<<ans<<'\n';
return;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>m;
if(n == 1){
sub1();
return 0;
}
sub2();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
980 KB |
Output is correct |
2 |
Correct |
3 ms |
980 KB |
Output is correct |
3 |
Correct |
6 ms |
980 KB |
Output is correct |
4 |
Correct |
8 ms |
1096 KB |
Output is correct |
5 |
Correct |
8 ms |
980 KB |
Output is correct |
6 |
Correct |
7 ms |
976 KB |
Output is correct |
7 |
Correct |
8 ms |
1100 KB |
Output is correct |
8 |
Correct |
10 ms |
980 KB |
Output is correct |
9 |
Correct |
10 ms |
972 KB |
Output is correct |
10 |
Correct |
12 ms |
1100 KB |
Output is correct |
11 |
Correct |
13 ms |
1104 KB |
Output is correct |
12 |
Correct |
14 ms |
980 KB |
Output is correct |
13 |
Correct |
15 ms |
1104 KB |
Output is correct |
14 |
Correct |
19 ms |
1112 KB |
Output is correct |
15 |
Correct |
16 ms |
1096 KB |
Output is correct |
16 |
Correct |
17 ms |
1100 KB |
Output is correct |
17 |
Correct |
16 ms |
1096 KB |
Output is correct |
18 |
Correct |
16 ms |
1100 KB |
Output is correct |
19 |
Correct |
16 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
980 KB |
Output is correct |
12 |
Correct |
3 ms |
980 KB |
Output is correct |
13 |
Correct |
6 ms |
980 KB |
Output is correct |
14 |
Correct |
8 ms |
1096 KB |
Output is correct |
15 |
Correct |
8 ms |
980 KB |
Output is correct |
16 |
Correct |
7 ms |
976 KB |
Output is correct |
17 |
Correct |
8 ms |
1100 KB |
Output is correct |
18 |
Correct |
10 ms |
980 KB |
Output is correct |
19 |
Correct |
10 ms |
972 KB |
Output is correct |
20 |
Correct |
12 ms |
1100 KB |
Output is correct |
21 |
Correct |
13 ms |
1104 KB |
Output is correct |
22 |
Correct |
14 ms |
980 KB |
Output is correct |
23 |
Correct |
15 ms |
1104 KB |
Output is correct |
24 |
Correct |
19 ms |
1112 KB |
Output is correct |
25 |
Correct |
16 ms |
1096 KB |
Output is correct |
26 |
Correct |
17 ms |
1100 KB |
Output is correct |
27 |
Correct |
16 ms |
1096 KB |
Output is correct |
28 |
Correct |
16 ms |
1100 KB |
Output is correct |
29 |
Correct |
16 ms |
980 KB |
Output is correct |
30 |
Runtime error |
2 ms |
2004 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
980 KB |
Output is correct |
12 |
Correct |
3 ms |
980 KB |
Output is correct |
13 |
Correct |
6 ms |
980 KB |
Output is correct |
14 |
Correct |
8 ms |
1096 KB |
Output is correct |
15 |
Correct |
8 ms |
980 KB |
Output is correct |
16 |
Correct |
7 ms |
976 KB |
Output is correct |
17 |
Correct |
8 ms |
1100 KB |
Output is correct |
18 |
Correct |
10 ms |
980 KB |
Output is correct |
19 |
Correct |
10 ms |
972 KB |
Output is correct |
20 |
Correct |
12 ms |
1100 KB |
Output is correct |
21 |
Correct |
13 ms |
1104 KB |
Output is correct |
22 |
Correct |
14 ms |
980 KB |
Output is correct |
23 |
Correct |
15 ms |
1104 KB |
Output is correct |
24 |
Correct |
19 ms |
1112 KB |
Output is correct |
25 |
Correct |
16 ms |
1096 KB |
Output is correct |
26 |
Correct |
17 ms |
1100 KB |
Output is correct |
27 |
Correct |
16 ms |
1096 KB |
Output is correct |
28 |
Correct |
16 ms |
1100 KB |
Output is correct |
29 |
Correct |
16 ms |
980 KB |
Output is correct |
30 |
Runtime error |
2 ms |
2004 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |