#include <bits/stdc++.h>
#define ll long long
#define debug(x) cerr<<x<<endl
using namespace std;
const int mxn = 55;
const ll inf = 1e18;
int n,m;
ll A[mxn][mxn];
ll memo[mxn][mxn][mxn][mxn];
ll pref[mxn][mxn];
ll f(int x1,int x2,int y1,int y2){
if(x1 == x2 && y1 == y2)return 0;
if(memo[x1][x2][y1][y2] != -1)return memo[x1][x2][y1][y2];
ll ans = inf;
//Cut horizontally
for(int i = x1;i<x2;i++){
ans = min(ans, f(x1,i,y1,y2) + f(i+1,x2,y1,y2));
}
//Cut vetically
for(int i = y1;i<y2;i++){
ans = min(ans, f(x1,x2,y1,i) + f(x1,x2,i+1,y2));
}
return memo[x1][x2][y1][y2] = ans + (pref[x2][y2] - pref[x1-1][y2] - pref[x2][y1-1] + pref[x1-1][y1-1]);
}
void solve(int T){
cin>>n>>m;
for(int i = 1;i<=n;i++){
for(int j = 1;j<=m;j++){
cin>>A[i][j];
}
}
memset(memo,-1,sizeof(memo));
memset(pref,0,sizeof(pref));
for(int i = 1;i<=n;i++){
for(int j = 1;j<=m;j++){
pref[i][j] = pref[i-1][j] + pref[i][j-1] + A[i][j] - pref[i-1][j-1];
}
}
cout<<f(1,n,1,m)<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin>>t;
for(int i = 1;i<=t;i++)solve(i);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
71844 KB |
Output is correct |
2 |
Correct |
31 ms |
71956 KB |
Output is correct |
3 |
Correct |
28 ms |
71964 KB |
Output is correct |
4 |
Correct |
31 ms |
71860 KB |
Output is correct |
5 |
Correct |
32 ms |
71912 KB |
Output is correct |
6 |
Correct |
28 ms |
71880 KB |
Output is correct |
7 |
Correct |
32 ms |
71936 KB |
Output is correct |
8 |
Correct |
33 ms |
71948 KB |
Output is correct |
9 |
Correct |
40 ms |
71868 KB |
Output is correct |
10 |
Correct |
40 ms |
71884 KB |
Output is correct |
11 |
Correct |
33 ms |
72056 KB |
Output is correct |
12 |
Correct |
45 ms |
71888 KB |
Output is correct |
13 |
Correct |
66 ms |
71912 KB |
Output is correct |
14 |
Correct |
37 ms |
71884 KB |
Output is correct |
15 |
Correct |
67 ms |
71984 KB |
Output is correct |
16 |
Correct |
33 ms |
71900 KB |
Output is correct |
17 |
Correct |
41 ms |
71884 KB |
Output is correct |
18 |
Correct |
159 ms |
71884 KB |
Output is correct |
19 |
Correct |
224 ms |
71984 KB |
Output is correct |
20 |
Correct |
248 ms |
71992 KB |
Output is correct |