#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define ll long long
const int MAX=51, MAXV=1e9+10;
int n, m, dp[MAX][MAX][MAX][MAX], p[MAX][MAX], t[MAX][MAX];
int pref(int x1, int y1, int x2, int y2){
return p[x2][y2]+p[x1-1][y1-1]-p[x1-1][y2]-p[x2][y1-1];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin>>n>>m;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
cin>>t[i][j];
p[i][j]=p[i-1][j]+p[i][j-1]-p[i-1][j-1]+t[i][j];
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(i==1 && j==1) continue;
for(int x=1; x<=n-i+1; x++){
for(int y=1; y<=m-j+1; y++){
dp[x][y][x+i-1][y+j-1]=MAXV;
//pion
for(int l=y; l<=y+j-2; l++)
dp[x][y][x+i-1][y+j-1]=min(dp[x][y][x+i-1][y+j-1], dp[x][y][x+i-1][l]+dp[x][l+1][x+i-1][y+j-1]);
//poziom
for(int l=x; l<=x+i-2; l++)
dp[x][y][x+i-1][y+j-1]=min(dp[x][y][x+i-1][y+j-1], dp[x][y][l][y+j-1]+dp[l+1][y][x+i-1][y+j-1]);
dp[x][y][x+i-1][y+j-1]+=pref(x, y, x+i-1, y+j-1);
}
}
}
}
cout<<dp[1][1][n][m]<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
844 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
3 ms |
2656 KB |
Output is correct |
9 |
Correct |
4 ms |
3668 KB |
Output is correct |
10 |
Correct |
6 ms |
4300 KB |
Output is correct |
11 |
Correct |
5 ms |
3788 KB |
Output is correct |
12 |
Correct |
17 ms |
7372 KB |
Output is correct |
13 |
Correct |
29 ms |
9392 KB |
Output is correct |
14 |
Correct |
7 ms |
4544 KB |
Output is correct |
15 |
Correct |
36 ms |
10428 KB |
Output is correct |
16 |
Correct |
4 ms |
3552 KB |
Output is correct |
17 |
Correct |
23 ms |
7232 KB |
Output is correct |
18 |
Correct |
147 ms |
15940 KB |
Output is correct |
19 |
Correct |
190 ms |
19524 KB |
Output is correct |
20 |
Correct |
243 ms |
21016 KB |
Output is correct |