#include <iostream>
#define MAXN 55
using namespace std;
typedef long long int lld;
int N, M, choc[MAXN][MAXN], pre[MAXN][MAXN], DP[MAXN][MAXN][MAXN][MAXN];
int sum ( int x1, int y1, int x2, int y2 )
{
return pre[x2][y2] - pre[x1 - 1][y2] - pre[x2][y1 - 1] + pre[x1 - 1][y1 - 1];
}
int dp ( int x1, int y1, int x2, int y2 )
{
if ( x1 == x2 && y1 == y2 )
return 0;
if ( DP[x1][y1][x2][y2] )
return DP[x1][y1][x2][y2];
int aux = (1 << 30);
if ( x1 < x2 )
for ( int i = x1; i < x2; i++ )
aux = min ( aux, dp ( x1, y1, i, y2 ) + dp ( i + 1, y1, x2, y2 ) );
if ( y1 < y2 )
for ( int i = y1; i < y2; i++ )
aux = min ( aux, dp ( x1, y1, x2, i ) + dp ( x1, i + 1, x2, y2 ) );
DP[x1][y1][x2][y2] = aux + sum ( x1, y1, x2, y2 );
//cout << "returning " << DP[x1][y1][x2][y2] << " for " << x1 << " " << y1 << " " << x2 << " " << y2 << "\n";
return DP[x1][y1][x2][y2];
}
int main ()
{
cin >> N >> M;
for ( int i = 1; i <= N; i++ )
{
for ( int j = 1; j <= M; j++ )
{
cin >> choc[i][j];
pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] + choc[i][j];
}
}
cout << dp ( 1, 1, N, M ) << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
11 ms |
2816 KB |
Output is correct |
9 |
Correct |
19 ms |
3840 KB |
Output is correct |
10 |
Correct |
28 ms |
4472 KB |
Output is correct |
11 |
Correct |
23 ms |
3840 KB |
Output is correct |
12 |
Correct |
88 ms |
7672 KB |
Output is correct |
13 |
Correct |
145 ms |
9848 KB |
Output is correct |
14 |
Correct |
38 ms |
4728 KB |
Output is correct |
15 |
Correct |
181 ms |
11000 KB |
Output is correct |
16 |
Correct |
17 ms |
3968 KB |
Output is correct |
17 |
Correct |
75 ms |
7800 KB |
Output is correct |
18 |
Correct |
479 ms |
17344 KB |
Output is correct |
19 |
Correct |
750 ms |
21228 KB |
Output is correct |
20 |
Correct |
870 ms |
22904 KB |
Output is correct |