#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll int MAX = 1e18;
ll int dp[51][51][51][51];
ll int a[51][51], ps[51][51];
int n, m;
void input() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
}
void init() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
for (int k = 1; k <= n; k++) {
for (int l = 1; l <= m; l++) {
dp[i][j][k][l] = -1;
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
ps[i][j] = (ps[i][j - 1] + ps[i - 1][j] - ps[i - 1][j - 1]) + a[i][j];
}
}
}
ll int recur(int x1, int y1, int x2, int y2) {
if (dp[x1][y1][x2][y2] != -1) return dp[x1][y1][x2][y2];
if (x1 == x2 && y1 == y2) return dp[x1][y1][x2][y2] = 0;
dp[x1][y1][x2][y2] = MAX;
for (int x = x1; x < x2; x++) {
ll int cur = recur(x1, y1, x, y2) + recur(x + 1, y1, x2, y2);
dp[x1][y1][x2][y2] = min(dp[x1][y1][x2][y2], cur);
}
for (int y = y1; y < y2; y++) {
ll int cur = recur(x1, y1, x2, y) + recur(x1, y + 1, x2, y2);
dp[x1][y1][x2][y2] = min(dp[x1][y1][x2][y2], cur);
}
dp[x1][y1][x2][y2] += ps[x2][y2] - ps[x1 - 1][y2] - ps[x2][y1 - 1] + ps[x1 - 1][y1 - 1];
return dp[x1][y1][x2][y2];
}
int main() {
input();
init();
cout << recur(1, 1, n, m) << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
8 |
Correct |
12 ms |
4896 KB |
Output is correct |
9 |
Correct |
15 ms |
7348 KB |
Output is correct |
10 |
Correct |
24 ms |
8916 KB |
Output is correct |
11 |
Correct |
21 ms |
7124 KB |
Output is correct |
12 |
Correct |
62 ms |
16852 KB |
Output is correct |
13 |
Correct |
100 ms |
22256 KB |
Output is correct |
14 |
Correct |
30 ms |
9108 KB |
Output is correct |
15 |
Correct |
135 ms |
25228 KB |
Output is correct |
16 |
Correct |
14 ms |
8660 KB |
Output is correct |
17 |
Correct |
56 ms |
18308 KB |
Output is correct |
18 |
Correct |
322 ms |
39636 KB |
Output is correct |
19 |
Correct |
529 ms |
48380 KB |
Output is correct |
20 |
Correct |
648 ms |
51424 KB |
Output is correct |