#include <bits/stdc++.h>
using namespace std;
const int N = 51;
int m,n;
int s[N][N],dp[N][N][N][N];
bool visited[N][N][N][N];
int solve(int a,int b,int c,int d)
{
if(visited[a][b][c][d]) return dp[a][b][c][d];
visited[a][b][c][d] = true;
if(a==b and c==d) return 0;
dp[a][b][c][d] = INT_MAX;
for(int i = a;i < b;i++) dp[a][b][c][d] = min(dp[a][b][c][d],solve(a,i,c,d)+solve(i+1,b,c,d));
for(int i = c;i < d;i++) dp[a][b][c][d] = min(dp[a][b][c][d],solve(a,b,c,i)+solve(a,b,i+1,d));
return dp[a][b][c][d]+=s[b][d]-s[a-1][d]-s[b][c-1]+s[a-1][c-1];
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> m >> n;
for(int i = 1;i <= m;i++) for(int j = 1;j <= n;j++) cin >> s[i][j],s[i][j]+=s[i-1][j]+s[i][j-1]-s[i-1][j-1];
cout << solve(1,m,1,n);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
392 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
640 KB |
Output is correct |
6 |
Correct |
7 ms |
896 KB |
Output is correct |
7 |
Correct |
6 ms |
1312 KB |
Output is correct |
8 |
Correct |
21 ms |
2304 KB |
Output is correct |
9 |
Correct |
21 ms |
3704 KB |
Output is correct |
10 |
Correct |
37 ms |
4088 KB |
Output is correct |
11 |
Correct |
66 ms |
3040 KB |
Output is correct |
12 |
Correct |
133 ms |
7152 KB |
Output is correct |
13 |
Correct |
170 ms |
8484 KB |
Output is correct |
14 |
Correct |
119 ms |
3448 KB |
Output is correct |
15 |
Correct |
167 ms |
9464 KB |
Output is correct |
16 |
Correct |
44 ms |
7160 KB |
Output is correct |
17 |
Correct |
79 ms |
10104 KB |
Output is correct |
18 |
Correct |
434 ms |
14992 KB |
Output is correct |
19 |
Correct |
891 ms |
15712 KB |
Output is correct |
20 |
Correct |
944 ms |
16972 KB |
Output is correct |