#include<bits/stdc++.h>
using namespace std;
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 52, INF = 1e9 + 7;
int dp[N][N][N][N];
int a[N][N];
int sum(int x1, int y1, int x2, int y2) {
return a[x2][y2] - a[x1 - 1][y2] - a[x2][y1 - 1] + a[x1 - 1][y1 - 1];
}
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
cin >> a[i][j];
a[i][j] += a[i - 1][j] + a[i][j - 1];
a[i][j] -= a[i - 1][j - 1];
}
for (int x1 = n; x1; --x1) {
for (int y1 = m; y1; --y1) {
for (int x2 = x1; x2 <= n; ++x2) {
for (int y2 = y1; y2 <= m; ++y2) {
if (x1 == x2 && y1 == y2) {
dp[x1][y1][x2][y2] = 0;
continue;
}
int add = INF;
for (int i = x1; i < x2; ++i) {
add = min(add, dp[x1][y1][i][y2] + dp[i + 1][y1][x2][y2]);
}
for (int i = y1; i < y2; ++i) {
add = min(add, dp[x1][y1][x2][i] + dp[x1][i + 1][x2][y2]);
}
dp[x1][y1][x2][y2] = add + sum(x1, y1, x2, y2);
#ifdef HOME
cout << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << " : " << dp[x1][y1][x2][y2] << endl;
#endif
}
}
}
}
cout << dp[1][1][n][m] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
896 KB |
Output is correct |
7 |
Correct |
5 ms |
1024 KB |
Output is correct |
8 |
Correct |
7 ms |
2688 KB |
Output is correct |
9 |
Correct |
8 ms |
3712 KB |
Output is correct |
10 |
Correct |
9 ms |
4480 KB |
Output is correct |
11 |
Correct |
9 ms |
3840 KB |
Output is correct |
12 |
Correct |
16 ms |
7552 KB |
Output is correct |
13 |
Correct |
23 ms |
9576 KB |
Output is correct |
14 |
Correct |
11 ms |
4736 KB |
Output is correct |
15 |
Correct |
30 ms |
10624 KB |
Output is correct |
16 |
Correct |
8 ms |
3712 KB |
Output is correct |
17 |
Correct |
15 ms |
7424 KB |
Output is correct |
18 |
Correct |
60 ms |
16376 KB |
Output is correct |
19 |
Correct |
94 ms |
20088 KB |
Output is correct |
20 |
Correct |
105 ms |
21528 KB |
Output is correct |