#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> ps;
vector<vector<vector<vector<int>>>> dp;
int val(int a, int b, int c, int d){
return ps[b+1][d+1]-ps[a][d+1]-ps[b+1][c]+ps[a][c];
}
int solve(int a, int b, int c, int d){
if (a == b && c == d) return 0;
if (dp[a][b][c][d] != -1) return dp[a][b][c][d];
int bsf = pow(10, 8);
for (int i=a; i<b; i++) bsf = min(bsf, solve(a, i, c, d)+solve(i+1, b, c, d));
for (int i=c; i<d; i++) bsf = min(bsf, solve(a, b, c, i)+solve(a, b, i+1, d));
return dp[a][b][c][d] = bsf+val(a, b, c, d);
}
int main(){
int n, m;
cin >> n >> m;
ps.resize(n+1, vector<int>(m+1, 0));
for (int i=0; i<n; i++){
for (int j=0; j<m; j++){
cin >> ps[i+1][j+1];
ps[i+1][j+1] += (ps[i][j+1]+ps[i+1][j]-ps[i][j]);
}
}
dp.resize(n, vector<vector<vector<int>>>(n, vector<vector<int>>(m, vector<int>(m, -1))));
cout << solve(0, n-1, 0, m-1) << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
436 KB |
Output is correct |
8 |
Correct |
6 ms |
1372 KB |
Output is correct |
9 |
Correct |
10 ms |
1884 KB |
Output is correct |
10 |
Correct |
15 ms |
2396 KB |
Output is correct |
11 |
Correct |
13 ms |
2140 KB |
Output is correct |
12 |
Correct |
49 ms |
5720 KB |
Output is correct |
13 |
Correct |
88 ms |
8412 KB |
Output is correct |
14 |
Correct |
22 ms |
2908 KB |
Output is correct |
15 |
Correct |
99 ms |
9564 KB |
Output is correct |
16 |
Correct |
8 ms |
1880 KB |
Output is correct |
17 |
Correct |
39 ms |
4956 KB |
Output is correct |
18 |
Correct |
291 ms |
18524 KB |
Output is correct |
19 |
Correct |
411 ms |
26456 KB |
Output is correct |
20 |
Correct |
490 ms |
29492 KB |
Output is correct |