#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define f first
#define s second
#define int ll
using namespace std;
const int N = 55,inf = 1e18;
int a[N][N],dp[N][N][N][N];
int get(int l1,int r1,int l2,int r2){
if (dp[l1][r1][l2][r2] != inf) return dp[l1][r1][l2][r2];
if (l1 == l2 && r1 == r2) return dp[l1][r1][l2][r2] = 0;
int sum=0,ans=inf;
for (int i=l1;i<=l2;i++)
for (int j=r1;j<=r2;j++)
sum+=a[i][j];
for (int i = l1; i < l2; i++){
int res = sum + get(l1,r1,i,r2) + get(i + 1,r1,l2,r2);
ans = min(ans,res);
}
for (int j = r1; j < r2; j++){
int res = sum + get(l1,r1,l2,j) + get(l1,j + 1,l2,r2);
ans = min(ans,res);
}
return dp[l1][r1][l2][r2] = ans;
}
signed main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n,m;
cin>>n>>m;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
cin>>a[i][j];
for (int l1=1;l1<=n;l1++)
for (int r1=1;r1<=m;r1++)
for (int l2=l1;l2<=n;l2++)
for (int r2 =r1;r2<=m;r2++)
dp[l1][r1][l2][r2] = inf;
cout<<get(1,1,n,m)<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
1236 KB |
Output is correct |
8 |
Correct |
12 ms |
3540 KB |
Output is correct |
9 |
Correct |
18 ms |
5076 KB |
Output is correct |
10 |
Correct |
42 ms |
6100 KB |
Output is correct |
11 |
Correct |
22 ms |
5076 KB |
Output is correct |
12 |
Correct |
91 ms |
11128 KB |
Output is correct |
13 |
Correct |
257 ms |
14432 KB |
Output is correct |
14 |
Correct |
36 ms |
6356 KB |
Output is correct |
15 |
Correct |
193 ms |
16224 KB |
Output is correct |
16 |
Correct |
24 ms |
5684 KB |
Output is correct |
17 |
Correct |
89 ms |
11732 KB |
Output is correct |
18 |
Correct |
544 ms |
27484 KB |
Output is correct |
19 |
Correct |
879 ms |
34032 KB |
Output is correct |
20 |
Correct |
1052 ms |
37128 KB |
Output is correct |