#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define ll long long
#define eps 1e-7
#define all(x) ((x).begin()),((x).end())
#define usecppio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
#define int ll
using pii = pair<int, int>;
int n, m;
int r[52][52];
int pr[52][52];
int s[52][52][52][52];
int dp[52][52][52][52];
int solve(int r1, int c1, int r2, int c2) {
if (r1 > r2 || c1 > c2) return 0;
if (r1 == r2 && c1 == c2) return 0;
if (dp[r1][c1][r2][c2]) return dp[r1][c1][r2][c2];
int val = INT_MAX;
int tot = s[r1][c1][r2][c2];
for (int i = r1; i < r2; i++)
val = min(val, tot + solve(r1, c1, i, c2) + solve(i+1, c1, r2, c2));
for (int i = c1; i < c2; i++)
val = min(val, tot + solve(r1, c1, r2, i) + solve(r1, i+1, r2, c2));
return dp[r1][c1][r2][c2] = val;
}
int32_t main() {
usecppio
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> r[i][j];
pr[i][j] = pr[i-1][j] + pr[i][j-1] - pr[i-1][j-1] + r[i][j];
}
}
for (int r2 = 1; r2 <= n; r2++)
for (int c2 = 1; c2 <= m; c2++)
for (int r1 = 1; r1 <= r2; r1++)
for (int c1 = 1; c1 <= c2; c1++)
s[r1][c1][r2][c2] = pr[r2][c2]-pr[r2][c1-1]-pr[r1-1][c2]+pr[r1-1][c1-1];
cout << solve(1, 1, n, m) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
2 ms |
1612 KB |
Output is correct |
7 |
Correct |
2 ms |
1996 KB |
Output is correct |
8 |
Correct |
11 ms |
6632 KB |
Output is correct |
9 |
Correct |
18 ms |
9584 KB |
Output is correct |
10 |
Correct |
21 ms |
11584 KB |
Output is correct |
11 |
Correct |
17 ms |
9668 KB |
Output is correct |
12 |
Correct |
57 ms |
21068 KB |
Output is correct |
13 |
Correct |
91 ms |
27544 KB |
Output is correct |
14 |
Correct |
26 ms |
11928 KB |
Output is correct |
15 |
Correct |
145 ms |
31244 KB |
Output is correct |
16 |
Correct |
13 ms |
10520 KB |
Output is correct |
17 |
Correct |
53 ms |
22340 KB |
Output is correct |
18 |
Correct |
281 ms |
52036 KB |
Output is correct |
19 |
Correct |
468 ms |
64308 KB |
Output is correct |
20 |
Correct |
540 ms |
70148 KB |
Output is correct |