#include <bits/stdc++.h>
using namespace std;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
const int inf = 1e9 + 20;
const int maxn = 55;
int dp[maxn][maxn][maxn][maxn];
int pref[maxn][maxn];
void just_do_it() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> pref[i][j];
pref[i][j] += pref[i][j - 1];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
pref[i][j] += pref[i - 1][j];
}
}
for (int h = 1; h <= n; h++) {
for (int w = 1; w <= m; w++) {
if (h == 1 && w == 1) {
continue;
}
for (int lx = 1, rx = h; rx <= n; lx++, rx++) {
for (int ly = 1, ry = w; ry <= m; ly++, ry++) {
int sum = pref[rx][ry] - pref[rx][ly - 1] - pref[lx - 1][ry] + pref[lx - 1][ly - 1];
dp[lx][rx][ly][ry] = inf;
for (int k = lx; k < rx; k++) {
dp[lx][rx][ly][ry] = min(dp[lx][rx][ly][ry], sum + dp[lx][k][ly][ry] + dp[k + 1][rx][ly][ry]);
}
for (int k = ly; k < ry; k++) {
dp[lx][rx][ly][ry] = min(dp[lx][rx][ly][ry], sum + dp[lx][rx][ly][k] + dp[lx][rx][k + 1][ry]);
}
}
}
}
}
cout << dp[1][n][1][m];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 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 |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
2 ms |
1748 KB |
Output is correct |
9 |
Correct |
3 ms |
2884 KB |
Output is correct |
10 |
Correct |
4 ms |
3284 KB |
Output is correct |
11 |
Correct |
4 ms |
2388 KB |
Output is correct |
12 |
Correct |
16 ms |
5948 KB |
Output is correct |
13 |
Correct |
30 ms |
7536 KB |
Output is correct |
14 |
Correct |
6 ms |
2884 KB |
Output is correct |
15 |
Correct |
29 ms |
8492 KB |
Output is correct |
16 |
Correct |
4 ms |
5196 KB |
Output is correct |
17 |
Correct |
13 ms |
7880 KB |
Output is correct |
18 |
Correct |
104 ms |
13740 KB |
Output is correct |
19 |
Correct |
174 ms |
14412 KB |
Output is correct |
20 |
Correct |
178 ms |
15588 KB |
Output is correct |