#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int n,m,inf=numeric_limits<int>::max(); cin >> n >> m;
vector A(n+1,vector<int>(m+1)); for (int i(0);i < n;++i) for (int k(0);k < m;++k){
cin>>A[i+1][k+1]; A[i+1][k+1] += A[i][k+1]+A[i+1][k]-A[i][k];
}
vector dp(n+1,vector(n+1,vector(m+1,vector<int>(m+1,inf))));
for (int i(0);i < n;++i) for (int k(0);k < m;++k) dp[i][i+1][k][k+1] = 0;
for (int j(1);j <= n;++j) for (int l(1);l <= m;++l) if (j+l!=2){
for (int i(0);i <= n-j;++i) for (int k(0);k <= m-l;++k){
int r = inf;
for (int t(1);t < j;++t) r = min(r,dp[i][i+t][k][k+l]+dp[i+t][i+j][k][k+l]);
for (int t(1);t < l;++t) r = min(r,dp[i][i+j][k][k+t]+dp[i][i+j][k+t][k+l]);
dp[i][i+j][k][k+l] = r+A[i+j][k+l]-A[i][k+l]-A[i+j][k]+A[i][k];
}
}
cout << dp[0][n][0][m] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
420 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
2396 KB |
Output is correct |
9 |
Correct |
4 ms |
3624 KB |
Output is correct |
10 |
Correct |
6 ms |
4700 KB |
Output is correct |
11 |
Correct |
5 ms |
4188 KB |
Output is correct |
12 |
Correct |
20 ms |
10840 KB |
Output is correct |
13 |
Correct |
36 ms |
15708 KB |
Output is correct |
14 |
Correct |
8 ms |
5720 KB |
Output is correct |
15 |
Correct |
39 ms |
18012 KB |
Output is correct |
16 |
Correct |
4 ms |
2888 KB |
Output is correct |
17 |
Correct |
19 ms |
9308 KB |
Output is correct |
18 |
Correct |
114 ms |
36876 KB |
Output is correct |
19 |
Correct |
243 ms |
53332 KB |
Output is correct |
20 |
Correct |
267 ms |
58964 KB |
Output is correct |