#include <iostream>
#include <vector>
#include <utility>
#include <climits>
using namespace std;
/*
2 3
2 7 5
1 9 5
*/
int main() {
int n, m; cin >> n >> m;
vector<vector<int>> nums(n, vector<int>(m));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> nums[i][j];
vector<vector<int>> ps(n + 1, vector<int>(m + 1));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
ps[i + 1][j + 1] = nums[i][j] + ps[i + 1][j] + ps[i][j + 1] - ps[i][j];
vector<vector<vector<vector<int>>>> dp(n, vector<vector<vector<int>>>(m, vector<vector<int>>(n, vector<int>(m))));
for (int a = n - 1; a >= 0; a--) {
for (int b = m - 1; b >= 0; b--) {
for (int c = a; c < n; c++) {
for (int d = b; d < m; d++) {
if (a == c && b == d) continue;
dp[a][b][c][d] = ps[n][m] + 1e9;
int raisins = ps[c + 1][d + 1] - ps[c + 1][b] - ps[a][d + 1] + ps[a][b];
for (int i = a; i < c; i++)
dp[a][b][c][d] = min(dp[a][b][c][d], raisins + dp[a][b][i][d] + dp[i + 1][b][c][d]);
for (int j = b; j < d; j++)
dp[a][b][c][d] = min(dp[a][b][c][d], raisins + dp[a][b][c][j] + dp[a][j + 1][c][d]);
}
}
}
}
cout << dp[0][0][n - 1][m - 1] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
1364 KB |
Output is correct |
9 |
Correct |
4 ms |
1876 KB |
Output is correct |
10 |
Correct |
6 ms |
2388 KB |
Output is correct |
11 |
Correct |
4 ms |
2132 KB |
Output is correct |
12 |
Correct |
16 ms |
5736 KB |
Output is correct |
13 |
Correct |
21 ms |
8300 KB |
Output is correct |
14 |
Correct |
6 ms |
2912 KB |
Output is correct |
15 |
Correct |
25 ms |
9476 KB |
Output is correct |
16 |
Correct |
3 ms |
1748 KB |
Output is correct |
17 |
Correct |
11 ms |
4804 KB |
Output is correct |
18 |
Correct |
65 ms |
18464 KB |
Output is correct |
19 |
Correct |
118 ms |
26560 KB |
Output is correct |
20 |
Correct |
132 ms |
29352 KB |
Output is correct |