#include <bits/stdc++.h>
using namespace std;
#define MAX 51
int memo[MAX][MAX][MAX][MAX];
int t[MAX][MAX];
int suma(int i, int j, int a, int b){
return t[a][b] - t[a][j - 1] - t[i - 1][b] + t[i - 1][j - 1];
}
int dp(int i, int j, int a, int b){
if (i == a and j == b){
return 0;
}else{
if (memo[i][j][a][b] != 0){
return memo[i][j][a][b];
}else{
memo[i][j][a][b] = 1000000000;
for (int k = i; k < a; k++){
memo[i][j][a][b] = min(memo[i][j][a][b], dp(i, j, k, b) + dp(k + 1, j, a, b) + suma(i, j, a, b));
}
for (int c = j; c < b; c++){
memo[i][j][a][b] = min(memo[i][j][a][b], dp(i, j, a, c) + dp(i, c + 1, a, b) + suma(i, j, a, b));
}
return memo[i][j][a][b];
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
cin >> t[i][j];
t[i][j] += t[i][j - 1] + t[i - 1][j] - t[i - 1][j - 1];
}
}
/*
for (int i = 1; i <= 50; i++){
for (int j = 1; j <= 50; j++){
for (int a = 1; a <= 50; a++){
for (int b = 1; b <= 50; b++){
memo[i][j][a][b] = 1000000000;
}
}
}
}*/
cout << dp(1, 1, n, m);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
876 KB |
Output is correct |
7 |
Correct |
2 ms |
1004 KB |
Output is correct |
8 |
Correct |
15 ms |
2668 KB |
Output is correct |
9 |
Correct |
26 ms |
3692 KB |
Output is correct |
10 |
Correct |
38 ms |
4332 KB |
Output is correct |
11 |
Correct |
32 ms |
3820 KB |
Output is correct |
12 |
Correct |
117 ms |
7404 KB |
Output is correct |
13 |
Correct |
200 ms |
9452 KB |
Output is correct |
14 |
Correct |
53 ms |
4844 KB |
Output is correct |
15 |
Correct |
249 ms |
10460 KB |
Output is correct |
16 |
Correct |
22 ms |
3692 KB |
Output is correct |
17 |
Correct |
103 ms |
7276 KB |
Output is correct |
18 |
Correct |
641 ms |
16108 KB |
Output is correct |
19 |
Correct |
1024 ms |
19692 KB |
Output is correct |
20 |
Correct |
1212 ms |
21228 KB |
Output is correct |