#include <bits/stdc++.h>
using namespace std;
int n, m, x, pre[55][55], dp[55][55][55][55];
int getsum(int a, int b, int c, int d){
a++,b++,c++,d++;
return pre[c][d]-pre[c][b-1]-pre[a-1][d]+pre[a-1][b-1];
}
int recur(int x1, int y1, int x2, int y2)
{
if(x1==x2 and y1==y2) return dp[x1][y1][x2][y2]=0;
if(dp[x1][y1][x2][y2]!=-1) return dp[x1][y1][x2][y2];
int ans = (int)1e9;
for(int x = x1; x < x2; x++)
ans = min(ans, recur(x1, y1, x, y2)+recur(x+1,y1,x2,y2)+getsum(x1,y1,x2,y2));
for(int y = y1; y < y2; y++)
ans = min(ans, recur(x1, y1, x2, y)+recur(x1,y+1,x2,y2)+getsum(x1,y1,x2,y2));
return dp[x1][y1][x2][y2]=ans;
}
int32_t main()
{
cin >> n >> m; memset(dp, -1, sizeof(dp));
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> x; pre[i+1][j+1]+=pre[i+1][j]+pre[i][j+1]-pre[i][j]+x;
}
}
cout << recur(0,0,n-1,m-1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
36052 KB |
Output is correct |
2 |
Correct |
15 ms |
36028 KB |
Output is correct |
3 |
Correct |
15 ms |
36108 KB |
Output is correct |
4 |
Correct |
15 ms |
36088 KB |
Output is correct |
5 |
Correct |
14 ms |
36108 KB |
Output is correct |
6 |
Correct |
17 ms |
36052 KB |
Output is correct |
7 |
Correct |
16 ms |
36000 KB |
Output is correct |
8 |
Correct |
23 ms |
36052 KB |
Output is correct |
9 |
Correct |
29 ms |
36012 KB |
Output is correct |
10 |
Correct |
35 ms |
36104 KB |
Output is correct |
11 |
Correct |
31 ms |
36100 KB |
Output is correct |
12 |
Correct |
75 ms |
36116 KB |
Output is correct |
13 |
Correct |
118 ms |
36112 KB |
Output is correct |
14 |
Correct |
44 ms |
36116 KB |
Output is correct |
15 |
Correct |
178 ms |
36132 KB |
Output is correct |
16 |
Correct |
25 ms |
36052 KB |
Output is correct |
17 |
Correct |
65 ms |
36124 KB |
Output is correct |
18 |
Correct |
347 ms |
36124 KB |
Output is correct |
19 |
Correct |
569 ms |
36052 KB |
Output is correct |
20 |
Correct |
666 ms |
36140 KB |
Output is correct |