#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (auto i = (l); i < (r); i++)
#define ForE(i, l, r) for (auto i = (l); i <= (r); i++)
#define FordE(i, l, r) for (auto i = (l); i >= (r); i--)
#define Fora(v, a) for (auto v: (a))
#define bend(a) (a).begin(), (a).end()
#define isz(a) ((signed)(a).size())
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pii>;
using vvi = vector <vi>;
const int N = 50 + 2;
int n, m;
int a[N][N];
int b[N][N];
int sumsubrect(int x1, int y1, int x2, int y2){
return b[x2][y2] - b[x2][y1 - 1] - b[x1 - 1][y2] + b[x1 - 1][y1 - 1];
}
int dp[N][N][N][N];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("KEK.inp", "r", stdin);
// freopen("KEK.out", "w", stdout);
cin >> n >> m;
ForE(i, 1, n){
ForE(j, 1, m){
cin >> a[i][j];
}
}
ForE(i, 1, n){
ForE(j, 1, m){
b[i][j] = b[i][j - 1] + b[i - 1][j] - b[i - 1][j - 1] + a[i][j];
}
}
ForE(lenx, 1, n){
ForE(x1, 1, n - lenx + 1){
int x2 = x1 + lenx - 1;
ForE(leny, 1, m){
ForE(y1, 1, m - leny + 1){
int y2 = y1 + leny - 1;
dp[x1][x2][y1][y2] = INT_MAX;
For(x, x1, x2){
dp[x1][x2][y1][y2] = min(dp[x1][x2][y1][y2], dp[x1][x][y1][y2] + dp[x + 1][x2][y1][y2]);
}
For(y, y1, y2){
dp[x1][x2][y1][y2] = min(dp[x1][x2][y1][y2], dp[x1][x2][y1][y] + dp[x1][x2][y + 1][y2]);
}
dp[x1][x2][y1][y2] += sumsubrect(x1, y1, x2, y2);
if (lenx == 1 and leny == 1){
dp[x1][x2][y1][y2] = 0;
}
}
}
}
}
cout << dp[1][n][1][m] << endl;
}
/*
==================================================+
INPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 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 |
924 KB |
Output is correct |
8 |
Correct |
2 ms |
1748 KB |
Output is correct |
9 |
Correct |
3 ms |
2828 KB |
Output is correct |
10 |
Correct |
4 ms |
3276 KB |
Output is correct |
11 |
Correct |
4 ms |
2388 KB |
Output is correct |
12 |
Correct |
11 ms |
5716 KB |
Output is correct |
13 |
Correct |
17 ms |
7024 KB |
Output is correct |
14 |
Correct |
5 ms |
2772 KB |
Output is correct |
15 |
Correct |
22 ms |
7756 KB |
Output is correct |
16 |
Correct |
4 ms |
5052 KB |
Output is correct |
17 |
Correct |
10 ms |
7636 KB |
Output is correct |
18 |
Correct |
60 ms |
12356 KB |
Output is correct |
19 |
Correct |
90 ms |
12928 KB |
Output is correct |
20 |
Correct |
111 ms |
13984 KB |
Output is correct |