#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
int const nmax = 50;
ll const inf = 1000000000000000000;
ll dp[1 + nmax][1 + nmax][1 + nmax][1 + nmax];
int v[1 + nmax][1 + nmax], sum[1 + nmax][1 + nmax];
int getsum(int x, int y, int x2, int y2){
return sum[x2][y2] - sum[x - 1][y2] - sum[x2][y - 1] + sum[x - 1][y - 1];
}
int main()
{
int n, m;
cin >> n >> m;
for(int i = 1;i <= n; i++)
for(int j = 1;j <= m; j++) {
cin >> v[i][j];
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + v[i][j];
}
for(int dif = 0; dif < n; dif++)
for(int i = 1; i <= n - dif; i++)
for(int dif2 = 0; dif2 < m; dif2++)
for(int i2 = 1; i2 <= m - dif2; i2++){
int j = i + dif;
int j2 = i2 + dif2;
dp[i][j][i2][j2] = inf;
if(i == j && i2 == j2) {
dp[i][j][i2][j2] = 0;
continue;
}
ll basic = getsum(i, i2, j, j2);
for(int k = i; k < j; k++)
dp[i][j][i2][j2] = min(dp[i][j][i2][j2], dp[i][k][i2][j2] + dp[k + 1][j][i2][j2] + basic);
for(int k = i2; k < j2; k++)
dp[i][j][i2][j2] = min(dp[i][j][i2][j2], dp[i][j][i2][k] + dp[i][j][k + 1][j2] + basic);
}
cout << dp[1][n][1][m];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
768 KB |
Output is correct |
7 |
Correct |
5 ms |
1280 KB |
Output is correct |
8 |
Correct |
7 ms |
2560 KB |
Output is correct |
9 |
Correct |
9 ms |
4096 KB |
Output is correct |
10 |
Correct |
10 ms |
4864 KB |
Output is correct |
11 |
Correct |
9 ms |
3584 KB |
Output is correct |
12 |
Correct |
20 ms |
8960 KB |
Output is correct |
13 |
Correct |
32 ms |
11648 KB |
Output is correct |
14 |
Correct |
15 ms |
4480 KB |
Output is correct |
15 |
Correct |
35 ms |
13176 KB |
Output is correct |
16 |
Correct |
9 ms |
6784 KB |
Output is correct |
17 |
Correct |
22 ms |
11520 KB |
Output is correct |
18 |
Correct |
136 ms |
23300 KB |
Output is correct |
19 |
Correct |
206 ms |
24568 KB |
Output is correct |
20 |
Correct |
244 ms |
26464 KB |
Output is correct |