#include <bits/stdc++.h>
using namespace std;
const int N_MAX = 50;
int N, M;
int R[N_MAX + 2][N_MAX + 2];
int sum[N_MAX + 2][N_MAX + 2];
int minCost[N_MAX + 2][N_MAX + 2][N_MAX + 2][N_MAX + 2];
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
cin >> R[i][j];
}
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
sum[i][j] = R[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
}
}
for (int i1 = N; i1 >= 1; i1--) {
for (int i2 = i1; i2 <= N; i2++) {
for (int j1 = M; j1 >= 1; j1--) {
for (int j2 = j1; j2 <= M; j2++) {
if (i1 == i2 && j1 == j2) {
minCost[i1][j1][i2][j2] = 0;
} else {
minCost[i1][j1][i2][j2] = INT_MAX;
for (int i = i1; i < i2; i++) {
minCost[i1][j1][i2][j2] = min(minCost[i1][j1][i2][j2],
minCost[i1][j1][i][j2] + minCost[i + 1][j1][i2][j2]);
}
for (int j = j1; j < j2; j++) {
minCost[i1][j1][i2][j2] = min(minCost[i1][j1][i2][j2],
minCost[i1][j1][i2][j] + minCost[i1][j + 1][i2][j2]);
}
minCost[i1][j1][i2][j2] +=
sum[i2][j2] - sum[i1 - 1][j2] - sum[i2][j1 - 1] + sum[i1 - 1][j1 - 1];
}
}
}
}
}
cout << minCost[1][1][N][M] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
2 ms |
2632 KB |
Output is correct |
9 |
Correct |
3 ms |
3668 KB |
Output is correct |
10 |
Correct |
5 ms |
4296 KB |
Output is correct |
11 |
Correct |
4 ms |
3796 KB |
Output is correct |
12 |
Correct |
11 ms |
7456 KB |
Output is correct |
13 |
Correct |
16 ms |
9444 KB |
Output is correct |
14 |
Correct |
5 ms |
4692 KB |
Output is correct |
15 |
Correct |
20 ms |
10516 KB |
Output is correct |
16 |
Correct |
3 ms |
3668 KB |
Output is correct |
17 |
Correct |
9 ms |
7388 KB |
Output is correct |
18 |
Correct |
56 ms |
16360 KB |
Output is correct |
19 |
Correct |
88 ms |
20044 KB |
Output is correct |
20 |
Correct |
109 ms |
21424 KB |
Output is correct |