#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, a[2049][2049];
const ll inf = 1e18;
void chmin (ll &x, ll y) {
if (x > y) x = y;
}
vector <vector <ll>> recurse (int l, int r) {
int sze = r - l + 1;
vector <vector <ll>> dp(sze, vector <ll> (sze, inf));
if (l == r - 1) {
dp[0][1] = dp[1][0] = a[l][r];
return dp;
}
sze >>= 1;
int mid = (l + r) >> 1;
auto g = recurse(l, mid), h = recurse(1 + mid, r);
for (int i = 0; i < sze; i++) {
for (int j = 0; j < sze; j++) {
ll mn = inf;
for (int k = 0; k < sze; k++) {
chmin(mn, g[i][k] + a[k + l][j + mid + 1]);
}
for (int k = 0; k < sze; k++) {
chmin(dp[i][k + sze], mn + h[j][k]);
}
}
}
sze <<= 1;
for (int i = 0; i < (sze >> 1); i++) for (int j = (sze >> 1); j < sze; j++) dp[j][i] = dp[i][j];
return dp;
}
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
if (n == 2) {
cout << a[1][2] << '\n';
return 0;
}
auto g = recurse(1, n / 2), h = recurse(n / 2 + 1, n);
ll mn[n / 2 + 1][2];
for (int i = 0; i <= n / 2; i++) {
for (int j = 0; j < 2; j++) {
mn[i][j] = inf;
}
}
ll ans = inf;
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < n / 2; j++) {
chmin(mn[i][0], g[i][j]);
chmin(mn[i][1], h[i][j]);
}
}
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < n / 2; j++) {
chmin(ans, mn[i][0] + mn[j][1] + a[i + 1][j + n / 2 + 1]);
}
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
2648 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
3 ms |
2648 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2604 KB |
Output is correct |
33 |
Correct |
2 ms |
2648 KB |
Output is correct |
34 |
Correct |
3 ms |
2648 KB |
Output is correct |
35 |
Correct |
3 ms |
2648 KB |
Output is correct |
36 |
Correct |
2 ms |
2652 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
39 |
Correct |
3 ms |
2652 KB |
Output is correct |
40 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
2648 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
3 ms |
2648 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2604 KB |
Output is correct |
33 |
Correct |
2 ms |
2648 KB |
Output is correct |
34 |
Correct |
3 ms |
2648 KB |
Output is correct |
35 |
Correct |
3 ms |
2648 KB |
Output is correct |
36 |
Correct |
2 ms |
2652 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
39 |
Correct |
3 ms |
2652 KB |
Output is correct |
40 |
Correct |
2 ms |
2652 KB |
Output is correct |
41 |
Correct |
33 ms |
5980 KB |
Output is correct |
42 |
Correct |
34 ms |
6040 KB |
Output is correct |
43 |
Correct |
41 ms |
6300 KB |
Output is correct |
44 |
Correct |
33 ms |
5968 KB |
Output is correct |
45 |
Correct |
39 ms |
5972 KB |
Output is correct |
46 |
Correct |
33 ms |
5796 KB |
Output is correct |
47 |
Correct |
33 ms |
6008 KB |
Output is correct |
48 |
Correct |
35 ms |
5908 KB |
Output is correct |
49 |
Correct |
33 ms |
5980 KB |
Output is correct |
50 |
Correct |
33 ms |
5976 KB |
Output is correct |
51 |
Correct |
32 ms |
5980 KB |
Output is correct |
52 |
Correct |
33 ms |
5924 KB |
Output is correct |
53 |
Correct |
37 ms |
6048 KB |
Output is correct |
54 |
Correct |
33 ms |
6060 KB |
Output is correct |
55 |
Correct |
33 ms |
5980 KB |
Output is correct |
56 |
Correct |
40 ms |
6032 KB |
Output is correct |
57 |
Correct |
32 ms |
5968 KB |
Output is correct |
58 |
Correct |
39 ms |
5932 KB |
Output is correct |
59 |
Correct |
36 ms |
5980 KB |
Output is correct |
60 |
Correct |
40 ms |
5932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
2648 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
3 ms |
2648 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2604 KB |
Output is correct |
33 |
Correct |
2 ms |
2648 KB |
Output is correct |
34 |
Correct |
3 ms |
2648 KB |
Output is correct |
35 |
Correct |
3 ms |
2648 KB |
Output is correct |
36 |
Correct |
2 ms |
2652 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
39 |
Correct |
3 ms |
2652 KB |
Output is correct |
40 |
Correct |
2 ms |
2652 KB |
Output is correct |
41 |
Correct |
33 ms |
5980 KB |
Output is correct |
42 |
Correct |
34 ms |
6040 KB |
Output is correct |
43 |
Correct |
41 ms |
6300 KB |
Output is correct |
44 |
Correct |
33 ms |
5968 KB |
Output is correct |
45 |
Correct |
39 ms |
5972 KB |
Output is correct |
46 |
Correct |
33 ms |
5796 KB |
Output is correct |
47 |
Correct |
33 ms |
6008 KB |
Output is correct |
48 |
Correct |
35 ms |
5908 KB |
Output is correct |
49 |
Correct |
33 ms |
5980 KB |
Output is correct |
50 |
Correct |
33 ms |
5976 KB |
Output is correct |
51 |
Correct |
32 ms |
5980 KB |
Output is correct |
52 |
Correct |
33 ms |
5924 KB |
Output is correct |
53 |
Correct |
37 ms |
6048 KB |
Output is correct |
54 |
Correct |
33 ms |
6060 KB |
Output is correct |
55 |
Correct |
33 ms |
5980 KB |
Output is correct |
56 |
Correct |
40 ms |
6032 KB |
Output is correct |
57 |
Correct |
32 ms |
5968 KB |
Output is correct |
58 |
Correct |
39 ms |
5932 KB |
Output is correct |
59 |
Correct |
36 ms |
5980 KB |
Output is correct |
60 |
Correct |
40 ms |
5932 KB |
Output is correct |
61 |
Correct |
1236 ms |
38860 KB |
Output is correct |
62 |
Correct |
1157 ms |
38916 KB |
Output is correct |
63 |
Correct |
1226 ms |
79340 KB |
Output is correct |
64 |
Correct |
1236 ms |
79328 KB |
Output is correct |
65 |
Correct |
1173 ms |
79332 KB |
Output is correct |
66 |
Correct |
1186 ms |
79300 KB |
Output is correct |
67 |
Correct |
1154 ms |
79232 KB |
Output is correct |
68 |
Correct |
1123 ms |
79324 KB |
Output is correct |
69 |
Correct |
1117 ms |
79296 KB |
Output is correct |
70 |
Correct |
1151 ms |
79328 KB |
Output is correct |
71 |
Correct |
1126 ms |
79964 KB |
Output is correct |
72 |
Correct |
1187 ms |
80200 KB |
Output is correct |
73 |
Correct |
1128 ms |
79912 KB |
Output is correct |
74 |
Correct |
1160 ms |
79704 KB |
Output is correct |
75 |
Correct |
1206 ms |
79976 KB |
Output is correct |
76 |
Correct |
1189 ms |
80132 KB |
Output is correct |
77 |
Correct |
1105 ms |
79960 KB |
Output is correct |
78 |
Correct |
1145 ms |
79908 KB |
Output is correct |
79 |
Correct |
1123 ms |
79964 KB |
Output is correct |
80 |
Correct |
1195 ms |
80160 KB |
Output is correct |