#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define ckmin(a, b) a = min(a, b)
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
vector<vector<vector<vector<int>>>> dp(n, vector<vector<vector<int>>>(m, vector<vector<int>>(n, vector<int>(m, INT_MAX/5))));
vector<vector<int>> sum(n + 1, vector<int>(m + 1));
vector<vector<int>> v(n, vector<int>(m));
sum.resize(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> v[i][j];
sum[i + 1][j + 1] = sum[i][j + 1] + sum[i + 1][j] - sum[i][j] + v[i][j];
dp[i][j][i][j] = 0;
}
}
for (int w = 0; w < n; w++)
for (int h = 0; h < m; h++)
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if(i + w >= n) continue;
if(j + h >= m) continue;
int s = sum[i + w + 1][j + h + 1] - sum[i + w + 1][j] - sum[i][j + h + 1] + sum[i][j];
for (int k = i; k < i + w; k++) {
ckmin(dp[i][j][i + w][j + h], dp[i][j][k][j + h] + dp[k + 1][j][i + w][j + h] + s);
}
for (int k = j; k < j + h; k++) {
ckmin(dp[i][j][i + w][j + h], dp[i][j][i + w][k] + dp[i][k + 1][i + w][j + h] + s);
}
}
cout << dp[0][0][n - 1][m - 1] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
3 ms |
1372 KB |
Output is correct |
9 |
Correct |
5 ms |
1884 KB |
Output is correct |
10 |
Correct |
7 ms |
2396 KB |
Output is correct |
11 |
Correct |
5 ms |
2244 KB |
Output is correct |
12 |
Correct |
25 ms |
5724 KB |
Output is correct |
13 |
Correct |
41 ms |
8348 KB |
Output is correct |
14 |
Correct |
9 ms |
2904 KB |
Output is correct |
15 |
Correct |
60 ms |
9560 KB |
Output is correct |
16 |
Correct |
4 ms |
1880 KB |
Output is correct |
17 |
Correct |
20 ms |
4700 KB |
Output is correct |
18 |
Correct |
181 ms |
18524 KB |
Output is correct |
19 |
Correct |
273 ms |
26716 KB |
Output is correct |
20 |
Correct |
354 ms |
29272 KB |
Output is correct |