#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<limits.h>
#include<math.h>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<iomanip>
#include<cstring>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
//const int MOD=1e9+7;
//typedef pair<ll,ll>Point;
//typedef pair<ll,ll>Line;
//#define x first
//#define y second
int mat[50][50];
int sum[51][51];
int w,h;
bool dpt[51][51][51][51];
int dp[51][51][51][51];
int get(int x1,int x2,int y1,int y2){
if(dpt[x1][x2][y1][y2])
return dp[x1][x2][y1][y2];
if(x1==x2-1&&y1==y2-1)
return 0;
int res=1e9;
for(int x=x1+1;x<x2;x++)
res=min(res,get(x1,x,y1,y2)+get(x,x2,y1,y2));
for(int y=y1+1;y<y2;y++)
res=min(res,get(x1,x2,y1,y)+get(x1,x2,y,y2));
res+=sum[x2][y2]+sum[x1][y1]-sum[x2][y1]-sum[x1][y2];
dpt[x1][x2][y1][y2]=true;
dp[x1][x2][y1][y2]=res;
return res;
}
void solve(){
cin>>h>>w;
for(int y=0;y<h;y++){
for(int x=0;x<w;x++){
cin>>mat[x][y];
}
}
for(int x=1;x<51;x++){
for(int y=1;y<51;y++){
sum[x][y]=mat[x-1][y-1]+sum[x-1][y]+sum[x][y-1]-sum[x-1][y-1];
}
}
cout<<get(0,w,0,h)<<"\n";
//cout<<get(0,1,0,2)<<"\n";
//cout<<sum[0][0]+sum[2][2]-sum[0][2]-sum[2][0]<<"\n";
}
int main(){
ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
//freopen("bank.in","r",stdin);freopen("bank.out","w",stdout);
int t=1;//cin>>t;
while(t--)solve();
return 0;
}
/*
2 3
2 7 5
1 9 5
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
2512 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
4564 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Correct |
1 ms |
4696 KB |
Output is correct |
8 |
Correct |
7 ms |
13908 KB |
Output is correct |
9 |
Correct |
8 ms |
13660 KB |
Output is correct |
10 |
Correct |
12 ms |
15760 KB |
Output is correct |
11 |
Correct |
11 ms |
18012 KB |
Output is correct |
12 |
Correct |
32 ms |
18412 KB |
Output is correct |
13 |
Correct |
53 ms |
20820 KB |
Output is correct |
14 |
Correct |
17 ms |
20572 KB |
Output is correct |
15 |
Correct |
65 ms |
20820 KB |
Output is correct |
16 |
Correct |
5 ms |
6744 KB |
Output is correct |
17 |
Correct |
24 ms |
13660 KB |
Output is correct |
18 |
Correct |
177 ms |
25480 KB |
Output is correct |
19 |
Correct |
260 ms |
28412 KB |
Output is correct |
20 |
Correct |
305 ms |
28756 KB |
Output is correct |