#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pb push_back
#define ll long long
using namespace std;
int a[2001][2001];
int dp[51][51][51][51];
int solve(int l , int r , int L , int R){
if(l == r && L == R){
return 0;
}
if(l >r || L > R){
return 0;
}
if(dp[l][r][L][R] != -1) return dp[l][r][L][R];
int sum =0;
for(int i =l ; i<=r; i++){
for(int j = L; j<=R; j++){
sum+=a[i][j];
}
}
int res = 1e9;
for(int i = l ; i<=r-1; i++){
res = min(res , solve(l , i , L , R) + solve(i + 1 , r , L ,R) + sum);
}
for(int i = L; i<=R-1; i++){
res = min(res , solve(l , r , L , i) + solve(l, r , i + 1 , R) + sum);
}
return dp[l][r][L][R] = res;
}
int main (){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n , m;
cin >> n >> m;
memset(dp , -1 , sizeof(dp));
for(int i = 1; i<=n; i++){
for(int j = 1; j<=m; j++){
cin >> a[i][j];
}
}
cout << solve(1 , n , 1 , m) << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
26752 KB |
Output is correct |
2 |
Correct |
16 ms |
26880 KB |
Output is correct |
3 |
Correct |
16 ms |
26880 KB |
Output is correct |
4 |
Correct |
16 ms |
26880 KB |
Output is correct |
5 |
Correct |
16 ms |
26880 KB |
Output is correct |
6 |
Correct |
16 ms |
26880 KB |
Output is correct |
7 |
Correct |
17 ms |
26880 KB |
Output is correct |
8 |
Correct |
28 ms |
26880 KB |
Output is correct |
9 |
Correct |
36 ms |
26880 KB |
Output is correct |
10 |
Correct |
46 ms |
26880 KB |
Output is correct |
11 |
Correct |
42 ms |
26880 KB |
Output is correct |
12 |
Correct |
122 ms |
27000 KB |
Output is correct |
13 |
Correct |
199 ms |
27128 KB |
Output is correct |
14 |
Correct |
60 ms |
26880 KB |
Output is correct |
15 |
Correct |
263 ms |
27028 KB |
Output is correct |
16 |
Correct |
35 ms |
27008 KB |
Output is correct |
17 |
Correct |
103 ms |
27008 KB |
Output is correct |
18 |
Correct |
659 ms |
27076 KB |
Output is correct |
19 |
Correct |
1068 ms |
27128 KB |
Output is correct |
20 |
Correct |
1272 ms |
27136 KB |
Output is correct |