#include <bits/stdc++.h>
using namespace std;
int a[50][50];
int d[50][50][50][50];
bool b[50][50][50][50];
int f(int p, int q, int r, int s){
if(p == r && q == s) {
return 0;
}
if (b[p][q][r][s]) {
return d[p][q][r][s];
}
else b[p][q][r][s] = 1;
int m = 1e9, t = 0;
for(int i = p; i <= r; i++){
for(int j = q; j <= s; j++){
t += a[i][j];
}
}
for(int i = p+1; i <= r; i++){
int x, y;
x = f(p, q, i-1, s);
y = f(i, q, r, s);
m = min(m, x + y + t);
}
for(int i = q+1; i <= s; i++){
int x, y;
x = f(p, q, r, i-1);
y = f(p, i, r, s);
m = min(m, x + y + t);
}
d[p][q][r][s] = m;
return m;
}
int main()
{
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> a[i][j];
}
}
cout << f(0, 0, n-1, m-1);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
9 ms |
3668 KB |
Output is correct |
9 |
Correct |
16 ms |
4892 KB |
Output is correct |
10 |
Correct |
29 ms |
5904 KB |
Output is correct |
11 |
Correct |
20 ms |
5124 KB |
Output is correct |
12 |
Correct |
71 ms |
9764 KB |
Output is correct |
13 |
Correct |
159 ms |
12268 KB |
Output is correct |
14 |
Correct |
31 ms |
6352 KB |
Output is correct |
15 |
Correct |
148 ms |
13592 KB |
Output is correct |
16 |
Correct |
12 ms |
4692 KB |
Output is correct |
17 |
Correct |
61 ms |
9408 KB |
Output is correct |
18 |
Correct |
383 ms |
20520 KB |
Output is correct |
19 |
Correct |
667 ms |
24924 KB |
Output is correct |
20 |
Correct |
748 ms |
26516 KB |
Output is correct |