#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll memo[55][55][55][55];
ll n,m,grid[55][55];
ll pref[55][55];
ll sum(ll a, ll b, ll c, ll d){
return pref[c][d] - pref[c][b - 1] - pref[a - 1][d] + pref[a - 1][b - 1];
}
ll dp (ll a, ll b, ll c, ll d){
if (memo[a][b][c][d] != -1) return memo[a][b][c][d];
if (a == c && b == d) return 0;
ll sv = 1e18;
for (int i = a; i <= c - 1; i ++){
sv = min (sv, dp (a, b, i, d) + dp (i + 1, b, c, d) + sum(a, b, c, d));
}
for (int i = b; i <= d - 1; i ++){
sv = min (sv, dp (a, b, c, i) + dp (a, i + 1, c, d) + sum(a, b, c, d));
}
memo[a][b][c][d] = sv;
return memo[a][b][c][d];
}
int main(){
memset(memo, -1 , sizeof(memo));
cin >> n >> m;
for (int i = 1; i <= n; i ++){
for (int j = 1; j <= m; j++){
cin >> grid[i][j];
}
}
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
pref[i][j] = pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1] + grid[i][j];
// cout <<pref[i][j];
}
}
cout << dp(1, 1, n, m);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
71892 KB |
Output is correct |
2 |
Correct |
27 ms |
71892 KB |
Output is correct |
3 |
Correct |
28 ms |
71996 KB |
Output is correct |
4 |
Correct |
27 ms |
71872 KB |
Output is correct |
5 |
Correct |
28 ms |
72016 KB |
Output is correct |
6 |
Correct |
28 ms |
71892 KB |
Output is correct |
7 |
Correct |
28 ms |
71892 KB |
Output is correct |
8 |
Correct |
29 ms |
71892 KB |
Output is correct |
9 |
Correct |
30 ms |
71892 KB |
Output is correct |
10 |
Correct |
33 ms |
71908 KB |
Output is correct |
11 |
Correct |
33 ms |
71960 KB |
Output is correct |
12 |
Correct |
44 ms |
71892 KB |
Output is correct |
13 |
Correct |
57 ms |
71892 KB |
Output is correct |
14 |
Correct |
35 ms |
71892 KB |
Output is correct |
15 |
Correct |
64 ms |
71956 KB |
Output is correct |
16 |
Correct |
30 ms |
71844 KB |
Output is correct |
17 |
Correct |
41 ms |
71892 KB |
Output is correct |
18 |
Correct |
127 ms |
72012 KB |
Output is correct |
19 |
Correct |
188 ms |
71892 KB |
Output is correct |
20 |
Correct |
233 ms |
71968 KB |
Output is correct |