#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9;
const int MAX = 1000 + 10;
int N;
ll d0;
vector<vector<ll>> dp(MAX*2,vector<ll>(MAX*2,INF));
vector<pair<ll,ll>> p;
int main() {
cin >> N;
for (int i = 0; i < 2*N; i++) {
ll X,Y,x,y; cin >> X >> Y;
if (Y <= 1) y = 1;
else y = 2;
if (X <= 1) x = 1;
else if (1 < X && X < N) x = X;
else x = N;
p.push_back(make_pair(x,y));
d0 += abs(Y-y)+abs(X-x);
}
sort(p.begin(),p.end());
// for (auto t: p) {
// cout << t.first << " " << t.second << "\n";
// }
dp[0][0] = 0;
for (int i = 1; i <= 2*N; i++) {
ll x,y; tie(x,y) = p[i-1];
for (int a = 0; a <= i; a++) {
dp[i][a] = dp[i-1][a] + abs(x-(i-a)) + abs(y-2);
if (a > 0) dp[i][a] = min(dp[i][a],dp[i-1][a-1] + abs(x - a) + abs(y-1));
}
}
cout << d0 + *min_element(dp[2*N].begin(),dp[2*N].begin()+2*N) << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
32376 KB |
Output is correct |
2 |
Correct |
33 ms |
32384 KB |
Output is correct |
3 |
Correct |
31 ms |
32384 KB |
Output is correct |
4 |
Correct |
29 ms |
32384 KB |
Output is correct |
5 |
Correct |
27 ms |
32380 KB |
Output is correct |
6 |
Correct |
28 ms |
32384 KB |
Output is correct |
7 |
Correct |
32 ms |
32376 KB |
Output is correct |
8 |
Correct |
32 ms |
32384 KB |
Output is correct |
9 |
Correct |
32 ms |
32376 KB |
Output is correct |
10 |
Correct |
30 ms |
32376 KB |
Output is correct |
11 |
Correct |
32 ms |
32384 KB |
Output is correct |
12 |
Correct |
29 ms |
32376 KB |
Output is correct |
13 |
Correct |
29 ms |
32384 KB |
Output is correct |
14 |
Correct |
29 ms |
32376 KB |
Output is correct |
15 |
Correct |
30 ms |
32376 KB |
Output is correct |
16 |
Correct |
32 ms |
32384 KB |
Output is correct |
17 |
Correct |
31 ms |
32376 KB |
Output is correct |
18 |
Correct |
30 ms |
32384 KB |
Output is correct |
19 |
Correct |
28 ms |
32316 KB |
Output is correct |
20 |
Correct |
30 ms |
32376 KB |
Output is correct |
21 |
Correct |
29 ms |
32376 KB |
Output is correct |
22 |
Correct |
27 ms |
32384 KB |
Output is correct |
23 |
Correct |
29 ms |
32376 KB |
Output is correct |
24 |
Correct |
27 ms |
32384 KB |
Output is correct |
25 |
Correct |
27 ms |
32384 KB |
Output is correct |
26 |
Correct |
27 ms |
32384 KB |
Output is correct |
27 |
Correct |
30 ms |
32324 KB |
Output is correct |
28 |
Correct |
27 ms |
32348 KB |
Output is correct |
29 |
Correct |
33 ms |
32376 KB |
Output is correct |
30 |
Correct |
26 ms |
32356 KB |
Output is correct |
31 |
Correct |
26 ms |
32384 KB |
Output is correct |
32 |
Correct |
34 ms |
32380 KB |
Output is correct |
33 |
Correct |
31 ms |
32384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
32376 KB |
Output is correct |
2 |
Correct |
33 ms |
32384 KB |
Output is correct |
3 |
Correct |
31 ms |
32384 KB |
Output is correct |
4 |
Correct |
29 ms |
32384 KB |
Output is correct |
5 |
Correct |
27 ms |
32380 KB |
Output is correct |
6 |
Correct |
28 ms |
32384 KB |
Output is correct |
7 |
Correct |
32 ms |
32376 KB |
Output is correct |
8 |
Correct |
32 ms |
32384 KB |
Output is correct |
9 |
Correct |
32 ms |
32376 KB |
Output is correct |
10 |
Correct |
30 ms |
32376 KB |
Output is correct |
11 |
Correct |
32 ms |
32384 KB |
Output is correct |
12 |
Correct |
29 ms |
32376 KB |
Output is correct |
13 |
Correct |
29 ms |
32384 KB |
Output is correct |
14 |
Correct |
29 ms |
32376 KB |
Output is correct |
15 |
Correct |
30 ms |
32376 KB |
Output is correct |
16 |
Correct |
32 ms |
32384 KB |
Output is correct |
17 |
Correct |
31 ms |
32376 KB |
Output is correct |
18 |
Correct |
30 ms |
32384 KB |
Output is correct |
19 |
Correct |
28 ms |
32316 KB |
Output is correct |
20 |
Correct |
30 ms |
32376 KB |
Output is correct |
21 |
Correct |
29 ms |
32376 KB |
Output is correct |
22 |
Correct |
27 ms |
32384 KB |
Output is correct |
23 |
Correct |
29 ms |
32376 KB |
Output is correct |
24 |
Correct |
27 ms |
32384 KB |
Output is correct |
25 |
Correct |
27 ms |
32384 KB |
Output is correct |
26 |
Correct |
27 ms |
32384 KB |
Output is correct |
27 |
Correct |
30 ms |
32324 KB |
Output is correct |
28 |
Correct |
27 ms |
32348 KB |
Output is correct |
29 |
Correct |
33 ms |
32376 KB |
Output is correct |
30 |
Correct |
26 ms |
32356 KB |
Output is correct |
31 |
Correct |
26 ms |
32384 KB |
Output is correct |
32 |
Correct |
34 ms |
32380 KB |
Output is correct |
33 |
Correct |
31 ms |
32384 KB |
Output is correct |
34 |
Correct |
34 ms |
32384 KB |
Output is correct |
35 |
Correct |
38 ms |
32452 KB |
Output is correct |
36 |
Correct |
37 ms |
32384 KB |
Output is correct |
37 |
Correct |
36 ms |
32504 KB |
Output is correct |
38 |
Correct |
33 ms |
32428 KB |
Output is correct |
39 |
Correct |
37 ms |
32372 KB |
Output is correct |
40 |
Correct |
36 ms |
32384 KB |
Output is correct |
41 |
Correct |
35 ms |
32384 KB |
Output is correct |
42 |
Correct |
32 ms |
32384 KB |
Output is correct |
43 |
Correct |
38 ms |
32404 KB |
Output is correct |
44 |
Correct |
39 ms |
32384 KB |
Output is correct |
45 |
Correct |
37 ms |
32376 KB |
Output is correct |
46 |
Correct |
37 ms |
32504 KB |
Output is correct |
47 |
Correct |
38 ms |
32376 KB |
Output is correct |
48 |
Correct |
33 ms |
32512 KB |
Output is correct |
49 |
Correct |
41 ms |
32512 KB |
Output is correct |
50 |
Correct |
35 ms |
32384 KB |
Output is correct |
51 |
Correct |
38 ms |
32504 KB |
Output is correct |
52 |
Correct |
34 ms |
32512 KB |
Output is correct |
53 |
Correct |
34 ms |
32504 KB |
Output is correct |
54 |
Correct |
33 ms |
32376 KB |
Output is correct |
55 |
Correct |
32 ms |
32376 KB |
Output is correct |
56 |
Correct |
34 ms |
32376 KB |
Output is correct |
57 |
Correct |
36 ms |
32472 KB |
Output is correct |
58 |
Correct |
33 ms |
32504 KB |
Output is correct |
59 |
Correct |
35 ms |
32384 KB |
Output is correct |
60 |
Correct |
37 ms |
32504 KB |
Output is correct |
61 |
Correct |
33 ms |
32384 KB |
Output is correct |
62 |
Correct |
32 ms |
32384 KB |
Output is correct |
63 |
Correct |
36 ms |
32460 KB |
Output is correct |
64 |
Correct |
45 ms |
32504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
32376 KB |
Output is correct |
2 |
Correct |
33 ms |
32384 KB |
Output is correct |
3 |
Correct |
31 ms |
32384 KB |
Output is correct |
4 |
Correct |
29 ms |
32384 KB |
Output is correct |
5 |
Correct |
27 ms |
32380 KB |
Output is correct |
6 |
Correct |
28 ms |
32384 KB |
Output is correct |
7 |
Correct |
32 ms |
32376 KB |
Output is correct |
8 |
Correct |
32 ms |
32384 KB |
Output is correct |
9 |
Correct |
32 ms |
32376 KB |
Output is correct |
10 |
Correct |
30 ms |
32376 KB |
Output is correct |
11 |
Correct |
32 ms |
32384 KB |
Output is correct |
12 |
Correct |
29 ms |
32376 KB |
Output is correct |
13 |
Correct |
29 ms |
32384 KB |
Output is correct |
14 |
Correct |
29 ms |
32376 KB |
Output is correct |
15 |
Correct |
30 ms |
32376 KB |
Output is correct |
16 |
Correct |
32 ms |
32384 KB |
Output is correct |
17 |
Correct |
31 ms |
32376 KB |
Output is correct |
18 |
Correct |
30 ms |
32384 KB |
Output is correct |
19 |
Correct |
28 ms |
32316 KB |
Output is correct |
20 |
Correct |
30 ms |
32376 KB |
Output is correct |
21 |
Correct |
29 ms |
32376 KB |
Output is correct |
22 |
Correct |
27 ms |
32384 KB |
Output is correct |
23 |
Correct |
29 ms |
32376 KB |
Output is correct |
24 |
Correct |
27 ms |
32384 KB |
Output is correct |
25 |
Correct |
27 ms |
32384 KB |
Output is correct |
26 |
Correct |
27 ms |
32384 KB |
Output is correct |
27 |
Correct |
30 ms |
32324 KB |
Output is correct |
28 |
Correct |
27 ms |
32348 KB |
Output is correct |
29 |
Correct |
33 ms |
32376 KB |
Output is correct |
30 |
Correct |
26 ms |
32356 KB |
Output is correct |
31 |
Correct |
26 ms |
32384 KB |
Output is correct |
32 |
Correct |
34 ms |
32380 KB |
Output is correct |
33 |
Correct |
31 ms |
32384 KB |
Output is correct |
34 |
Correct |
34 ms |
32384 KB |
Output is correct |
35 |
Correct |
38 ms |
32452 KB |
Output is correct |
36 |
Correct |
37 ms |
32384 KB |
Output is correct |
37 |
Correct |
36 ms |
32504 KB |
Output is correct |
38 |
Correct |
33 ms |
32428 KB |
Output is correct |
39 |
Correct |
37 ms |
32372 KB |
Output is correct |
40 |
Correct |
36 ms |
32384 KB |
Output is correct |
41 |
Correct |
35 ms |
32384 KB |
Output is correct |
42 |
Correct |
32 ms |
32384 KB |
Output is correct |
43 |
Correct |
38 ms |
32404 KB |
Output is correct |
44 |
Correct |
39 ms |
32384 KB |
Output is correct |
45 |
Correct |
37 ms |
32376 KB |
Output is correct |
46 |
Correct |
37 ms |
32504 KB |
Output is correct |
47 |
Correct |
38 ms |
32376 KB |
Output is correct |
48 |
Correct |
33 ms |
32512 KB |
Output is correct |
49 |
Correct |
41 ms |
32512 KB |
Output is correct |
50 |
Correct |
35 ms |
32384 KB |
Output is correct |
51 |
Correct |
38 ms |
32504 KB |
Output is correct |
52 |
Correct |
34 ms |
32512 KB |
Output is correct |
53 |
Correct |
34 ms |
32504 KB |
Output is correct |
54 |
Correct |
33 ms |
32376 KB |
Output is correct |
55 |
Correct |
32 ms |
32376 KB |
Output is correct |
56 |
Correct |
34 ms |
32376 KB |
Output is correct |
57 |
Correct |
36 ms |
32472 KB |
Output is correct |
58 |
Correct |
33 ms |
32504 KB |
Output is correct |
59 |
Correct |
35 ms |
32384 KB |
Output is correct |
60 |
Correct |
37 ms |
32504 KB |
Output is correct |
61 |
Correct |
33 ms |
32384 KB |
Output is correct |
62 |
Correct |
32 ms |
32384 KB |
Output is correct |
63 |
Correct |
36 ms |
32460 KB |
Output is correct |
64 |
Correct |
45 ms |
32504 KB |
Output is correct |
65 |
Runtime error |
203 ms |
72668 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
66 |
Halted |
0 ms |
0 KB |
- |