#include <iostream>
#include <vector>
using namespace std;
vector <vector <int>> grid;
vector <vector <int>> pfx;
const int maxn = 50;
int dp[maxn][maxn][maxn][maxn];
int solve(int r1, int r2, int c1, int c2) {
if (r1 == r2 && c1 == c2)
return 0;
if (dp[r1][r2][c1][c2] != -1)
return dp[r1][r2][c1][c2];
int ans = 1e9;
int x = pfx[r2][c2] - pfx[r2][c1-1] - pfx[r1-1][c2] + pfx[r2-1][c1-1];
for (int i = r1; i < r2; i++) {
ans = min(ans, solve(r1, i, c1, c2) + solve(i+1, r2, c1, c2) + x);
}
for (int i = c1; i < c2; i++) {
ans = min(ans, solve(r1, r2, c1, i) + solve(r1, r2, i+1, c2) + x);
}
dp[r1][r2][c1][c2] = ans;
return ans;
}
int main() {
int n, m;
cin >> n >> m;
grid = pfx = vector <vector <int>> (n+1, vector <int> (m+1));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
cin >> grid[i][j];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int l = 1; l <= m; l++) {
for (int k = 1; k <= m; k++)
dp[i][j][l][k] = -1;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
pfx[i][j] = grid[i][j];
pfx[i][j] += pfx[i-1][j] + pfx[i][j-1] - pfx[i-1][j-1];
}
}
cout << solve(1, n, 1, m) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Incorrect |
1 ms |
1492 KB |
Output isn't correct |
8 |
Incorrect |
3 ms |
2996 KB |
Output isn't correct |
9 |
Incorrect |
7 ms |
4948 KB |
Output isn't correct |
10 |
Incorrect |
7 ms |
5844 KB |
Output isn't correct |
11 |
Incorrect |
5 ms |
4052 KB |
Output isn't correct |
12 |
Correct |
16 ms |
10324 KB |
Output is correct |
13 |
Incorrect |
27 ms |
12244 KB |
Output isn't correct |
14 |
Incorrect |
7 ms |
4564 KB |
Output isn't correct |
15 |
Incorrect |
31 ms |
13652 KB |
Output isn't correct |
16 |
Incorrect |
6 ms |
9428 KB |
Output isn't correct |
17 |
Incorrect |
16 ms |
14420 KB |
Output isn't correct |
18 |
Incorrect |
72 ms |
22040 KB |
Output isn't correct |
19 |
Correct |
118 ms |
23064 KB |
Output is correct |
20 |
Runtime error |
27 ms |
48980 KB |
Execution killed with signal 11 |