#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
//#define INF 1145141919
#define INF 1000101441
#define MOD 1000000007
inline void chmin(int &x, int v) { if (x > v) x = v; }
int N, M;
vector<P> G[300000];
int dp[300][301];
int T[301];
signed main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> N >> M;
rep(i, N-1+M) {
int p, c;
cin >> p >> c;
p--;
G[p].pb(P(i+1, c));
}
if (N == 1) {
vector<int> costs;
for (P p : G[0]) costs.pb(p._2);
sort(all(costs));
int g = costs[costs.size()/2];
long long sum = 0;
for (int c : costs) sum += abs(c - g);
cout << sum << "\n";
}
else {
if (N+M > 300) return 1;
rep(i, N+M) rep(j, 301) dp[i][j] = INF;
for (int x=N; x<N+M; x++) dp[x][0] = 0;
for (int x=N-1; x>=0; x--) {
rep(j, 301) dp[x][j] = 0;
for (P p : G[x]) {
rep(j, 301) T[j] = INF;
int t = p._1, r = p._2;
for (int c=0; c<=300; c++) {
for (int l=0; c+l<=300; l++) {
chmin(T[c+l], dp[t][c] + abs(r-l));
}
}
rep(j, 301) dp[x][j] = min(dp[x][j]+T[j], INF);
}
}
int m = INF;
rep(i, 301) chmin(m, dp[0][i]);
cout << m << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
9560 KB |
Output is correct |
2 |
Correct |
0 ms |
9560 KB |
Output is correct |
3 |
Correct |
0 ms |
9560 KB |
Output is correct |
4 |
Correct |
0 ms |
9560 KB |
Output is correct |
5 |
Correct |
3 ms |
9560 KB |
Output is correct |
6 |
Correct |
3 ms |
9560 KB |
Output is correct |
7 |
Correct |
0 ms |
9560 KB |
Output is correct |
8 |
Correct |
3 ms |
9560 KB |
Output is correct |
9 |
Correct |
3 ms |
9560 KB |
Output is correct |
10 |
Correct |
0 ms |
9560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
9560 KB |
Output is correct |
2 |
Correct |
3 ms |
9560 KB |
Output is correct |
3 |
Correct |
9 ms |
9560 KB |
Output is correct |
4 |
Correct |
9 ms |
9560 KB |
Output is correct |
5 |
Correct |
16 ms |
9560 KB |
Output is correct |
6 |
Correct |
9 ms |
9560 KB |
Output is correct |
7 |
Correct |
16 ms |
9560 KB |
Output is correct |
8 |
Correct |
19 ms |
9560 KB |
Output is correct |
9 |
Correct |
16 ms |
9560 KB |
Output is correct |
10 |
Correct |
19 ms |
9560 KB |
Output is correct |
11 |
Correct |
23 ms |
9560 KB |
Output is correct |
12 |
Correct |
23 ms |
9560 KB |
Output is correct |
13 |
Correct |
23 ms |
9560 KB |
Output is correct |
14 |
Correct |
23 ms |
9560 KB |
Output is correct |
15 |
Correct |
29 ms |
9560 KB |
Output is correct |
16 |
Correct |
26 ms |
9560 KB |
Output is correct |
17 |
Correct |
29 ms |
9560 KB |
Output is correct |
18 |
Correct |
29 ms |
9560 KB |
Output is correct |
19 |
Correct |
26 ms |
9560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
9560 KB |
Output is correct |
2 |
Correct |
0 ms |
9560 KB |
Output is correct |
3 |
Correct |
0 ms |
9560 KB |
Output is correct |
4 |
Correct |
0 ms |
9560 KB |
Output is correct |
5 |
Correct |
3 ms |
9560 KB |
Output is correct |
6 |
Correct |
3 ms |
9560 KB |
Output is correct |
7 |
Correct |
0 ms |
9560 KB |
Output is correct |
8 |
Correct |
3 ms |
9560 KB |
Output is correct |
9 |
Correct |
3 ms |
9560 KB |
Output is correct |
10 |
Correct |
0 ms |
9560 KB |
Output is correct |
11 |
Correct |
3 ms |
9560 KB |
Output is correct |
12 |
Correct |
3 ms |
9560 KB |
Output is correct |
13 |
Correct |
9 ms |
9560 KB |
Output is correct |
14 |
Correct |
9 ms |
9560 KB |
Output is correct |
15 |
Correct |
16 ms |
9560 KB |
Output is correct |
16 |
Correct |
9 ms |
9560 KB |
Output is correct |
17 |
Correct |
16 ms |
9560 KB |
Output is correct |
18 |
Correct |
19 ms |
9560 KB |
Output is correct |
19 |
Correct |
16 ms |
9560 KB |
Output is correct |
20 |
Correct |
19 ms |
9560 KB |
Output is correct |
21 |
Correct |
23 ms |
9560 KB |
Output is correct |
22 |
Correct |
23 ms |
9560 KB |
Output is correct |
23 |
Correct |
23 ms |
9560 KB |
Output is correct |
24 |
Correct |
23 ms |
9560 KB |
Output is correct |
25 |
Correct |
29 ms |
9560 KB |
Output is correct |
26 |
Correct |
26 ms |
9560 KB |
Output is correct |
27 |
Correct |
29 ms |
9560 KB |
Output is correct |
28 |
Correct |
29 ms |
9560 KB |
Output is correct |
29 |
Correct |
26 ms |
9560 KB |
Output is correct |
30 |
Incorrect |
3 ms |
9560 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
9560 KB |
Output is correct |
2 |
Correct |
0 ms |
9560 KB |
Output is correct |
3 |
Correct |
0 ms |
9560 KB |
Output is correct |
4 |
Correct |
0 ms |
9560 KB |
Output is correct |
5 |
Correct |
3 ms |
9560 KB |
Output is correct |
6 |
Correct |
3 ms |
9560 KB |
Output is correct |
7 |
Correct |
0 ms |
9560 KB |
Output is correct |
8 |
Correct |
3 ms |
9560 KB |
Output is correct |
9 |
Correct |
3 ms |
9560 KB |
Output is correct |
10 |
Correct |
0 ms |
9560 KB |
Output is correct |
11 |
Correct |
3 ms |
9560 KB |
Output is correct |
12 |
Correct |
3 ms |
9560 KB |
Output is correct |
13 |
Correct |
9 ms |
9560 KB |
Output is correct |
14 |
Correct |
9 ms |
9560 KB |
Output is correct |
15 |
Correct |
16 ms |
9560 KB |
Output is correct |
16 |
Correct |
9 ms |
9560 KB |
Output is correct |
17 |
Correct |
16 ms |
9560 KB |
Output is correct |
18 |
Correct |
19 ms |
9560 KB |
Output is correct |
19 |
Correct |
16 ms |
9560 KB |
Output is correct |
20 |
Correct |
19 ms |
9560 KB |
Output is correct |
21 |
Correct |
23 ms |
9560 KB |
Output is correct |
22 |
Correct |
23 ms |
9560 KB |
Output is correct |
23 |
Correct |
23 ms |
9560 KB |
Output is correct |
24 |
Correct |
23 ms |
9560 KB |
Output is correct |
25 |
Correct |
29 ms |
9560 KB |
Output is correct |
26 |
Correct |
26 ms |
9560 KB |
Output is correct |
27 |
Correct |
29 ms |
9560 KB |
Output is correct |
28 |
Correct |
29 ms |
9560 KB |
Output is correct |
29 |
Correct |
26 ms |
9560 KB |
Output is correct |
30 |
Incorrect |
3 ms |
9560 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |