#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define F first
#define S second
const int N = 1087;
const ll inf = 9e18;
pii p[N*2];
ll f(int i,int x,int y)
{
return abs(x-p[i].F) + abs(y-p[i].S);
}
ll dp[N][N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
ll ans = 0;
int n;
cin >> n;
for (int i = 1; i <= 2*n; ++i) {
int x,y;
cin >> x >> y;
if (x < 1) ans += 1 - x, x = 1;
if (x > n) ans += x - n, x = n;
if (y < 1) ans += 1 - y, y = 1;
if (y > 2) ans += y - 2, y = 2;
p[i] = {x, y};
}
sort(p+1,p+1+2*n);
for (int t = 1; t <= 2*n; ++t)
for (int i = 0; i <= min(n,t); ++i) {
int j = t - i;
if (j > n)
continue;
dp[i][j] = inf;
if (i)
dp[i][j] = min(dp[i][j], dp[i-1][j] + f(t,i,1));
if (j)
dp[i][j] = min(dp[i][j], dp[i][j-1] + f(t,j,2));
}
cout << ans + dp[n][n] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
428 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
2 ms |
376 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
428 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
2 ms |
376 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
17 ms |
8952 KB |
Output is correct |
35 |
Correct |
18 ms |
8952 KB |
Output is correct |
36 |
Correct |
30 ms |
8952 KB |
Output is correct |
37 |
Correct |
31 ms |
8952 KB |
Output is correct |
38 |
Correct |
17 ms |
8952 KB |
Output is correct |
39 |
Correct |
18 ms |
8828 KB |
Output is correct |
40 |
Correct |
17 ms |
8956 KB |
Output is correct |
41 |
Correct |
19 ms |
8952 KB |
Output is correct |
42 |
Correct |
17 ms |
8828 KB |
Output is correct |
43 |
Correct |
17 ms |
8952 KB |
Output is correct |
44 |
Correct |
18 ms |
8952 KB |
Output is correct |
45 |
Correct |
18 ms |
8828 KB |
Output is correct |
46 |
Correct |
18 ms |
8952 KB |
Output is correct |
47 |
Correct |
18 ms |
8952 KB |
Output is correct |
48 |
Correct |
17 ms |
8952 KB |
Output is correct |
49 |
Correct |
20 ms |
8952 KB |
Output is correct |
50 |
Correct |
17 ms |
8952 KB |
Output is correct |
51 |
Correct |
19 ms |
8952 KB |
Output is correct |
52 |
Correct |
18 ms |
8952 KB |
Output is correct |
53 |
Correct |
18 ms |
8852 KB |
Output is correct |
54 |
Correct |
17 ms |
8952 KB |
Output is correct |
55 |
Correct |
18 ms |
8824 KB |
Output is correct |
56 |
Correct |
18 ms |
8952 KB |
Output is correct |
57 |
Correct |
18 ms |
8824 KB |
Output is correct |
58 |
Correct |
18 ms |
8824 KB |
Output is correct |
59 |
Correct |
17 ms |
8952 KB |
Output is correct |
60 |
Correct |
17 ms |
8952 KB |
Output is correct |
61 |
Correct |
18 ms |
8952 KB |
Output is correct |
62 |
Correct |
18 ms |
8956 KB |
Output is correct |
63 |
Correct |
17 ms |
8824 KB |
Output is correct |
64 |
Correct |
18 ms |
8952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
428 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
380 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
2 ms |
376 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
17 ms |
8952 KB |
Output is correct |
35 |
Correct |
18 ms |
8952 KB |
Output is correct |
36 |
Correct |
30 ms |
8952 KB |
Output is correct |
37 |
Correct |
31 ms |
8952 KB |
Output is correct |
38 |
Correct |
17 ms |
8952 KB |
Output is correct |
39 |
Correct |
18 ms |
8828 KB |
Output is correct |
40 |
Correct |
17 ms |
8956 KB |
Output is correct |
41 |
Correct |
19 ms |
8952 KB |
Output is correct |
42 |
Correct |
17 ms |
8828 KB |
Output is correct |
43 |
Correct |
17 ms |
8952 KB |
Output is correct |
44 |
Correct |
18 ms |
8952 KB |
Output is correct |
45 |
Correct |
18 ms |
8828 KB |
Output is correct |
46 |
Correct |
18 ms |
8952 KB |
Output is correct |
47 |
Correct |
18 ms |
8952 KB |
Output is correct |
48 |
Correct |
17 ms |
8952 KB |
Output is correct |
49 |
Correct |
20 ms |
8952 KB |
Output is correct |
50 |
Correct |
17 ms |
8952 KB |
Output is correct |
51 |
Correct |
19 ms |
8952 KB |
Output is correct |
52 |
Correct |
18 ms |
8952 KB |
Output is correct |
53 |
Correct |
18 ms |
8852 KB |
Output is correct |
54 |
Correct |
17 ms |
8952 KB |
Output is correct |
55 |
Correct |
18 ms |
8824 KB |
Output is correct |
56 |
Correct |
18 ms |
8952 KB |
Output is correct |
57 |
Correct |
18 ms |
8824 KB |
Output is correct |
58 |
Correct |
18 ms |
8824 KB |
Output is correct |
59 |
Correct |
17 ms |
8952 KB |
Output is correct |
60 |
Correct |
17 ms |
8952 KB |
Output is correct |
61 |
Correct |
18 ms |
8952 KB |
Output is correct |
62 |
Correct |
18 ms |
8956 KB |
Output is correct |
63 |
Correct |
17 ms |
8824 KB |
Output is correct |
64 |
Correct |
18 ms |
8952 KB |
Output is correct |
65 |
Runtime error |
4 ms |
760 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
66 |
Halted |
0 ms |
0 KB |
- |