#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
bool ckmin(T &t, const U& u) {
return u<t ? t=u,true : false;
}
int constexpr N = 52;
int r[N][N];
int ps[N][N];
int dp[N][N][N][N];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m; cin >> n >> m;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> r[i][j];
ps[i+1][j+1] = r[i][j] + ps[i+1][j] + ps[i][j+1] - ps[i][j];
}
}
for (int di = 0; di < n; ++di) {
for (int dj = 0; dj < m; ++dj) {
for (int li = 0; li < n-di; ++li) {
for (int lj = 0; lj < m-dj; ++lj) {
int ri = li + di, rj = lj + dj;
dp[li][lj][ri][rj] = (di+dj == 0 ? -r[li][lj] : 2e9);
for (int mi = li; mi < ri; ++mi) ckmin(dp[li][lj][ri][rj], dp[li][lj][mi][rj] + dp[mi+1][lj][ri][rj]);
for (int mj = lj; mj < rj; ++mj) ckmin(dp[li][lj][ri][rj], dp[li][lj][ri][mj] + dp[li][mj+1][ri][rj]);
dp[li][lj][ri][rj] += ps[ri+1][rj+1] - ps[ri+1][lj] - ps[li][rj+1] + ps[li][lj];
}
}
}
}
cout << dp[0][0][n-1][m-1] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
468 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 |
2644 KB |
Output is correct |
9 |
Correct |
4 ms |
3668 KB |
Output is correct |
10 |
Correct |
5 ms |
4308 KB |
Output is correct |
11 |
Correct |
7 ms |
3796 KB |
Output is correct |
12 |
Correct |
16 ms |
7508 KB |
Output is correct |
13 |
Correct |
23 ms |
9440 KB |
Output is correct |
14 |
Correct |
6 ms |
4692 KB |
Output is correct |
15 |
Correct |
36 ms |
10576 KB |
Output is correct |
16 |
Correct |
4 ms |
3668 KB |
Output is correct |
17 |
Correct |
17 ms |
7372 KB |
Output is correct |
18 |
Correct |
121 ms |
16312 KB |
Output is correct |
19 |
Correct |
174 ms |
19908 KB |
Output is correct |
20 |
Correct |
247 ms |
21412 KB |
Output is correct |