# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
252340 | SamAnd | Raisins (IOI09_raisins) | C++17 | 712 ms | 30484 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 55, INF = 1000000009;
int n, m;
int a[N][N];
int p[N][N];
void pre()
{
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
p[i][j] = p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1] + a[i][j];
}
}
}
int sum(int x1, int y1, int x2, int y2)
{
return p[x2][y2] - p[x1 - 1][y2] - p[x2][y1 - 1] + p[x1 - 1][y1 - 1];
}
bool c[N][N][N][N];
int dp[N][N][N][N];
int rec(int x1, int y1, int x2, int y2)
{
if (c[x1][y1][x2][y2])
return dp[x1][y1][x2][y2];
c[x1][y1][x2][y2] = true;
if (x1 == x2 && y1 == y2)
return 0;
dp[x1][y1][x2][y2] = INF;
int s = sum(x1, y1, x2, y2);
for (int i = x1; i < x2; ++i)
dp[x1][y1][x2][y2] = min(dp[x1][y1][x2][y2], s + rec(x1, y1, i, y2) + rec(i + 1, y1, x2, y2));
for (int j = y1; j < y2; ++j)
dp[x1][y1][x2][y2] = min(dp[x1][y1][x2][y2], s + rec(x1, y1, x2, j) + rec(x1, j + 1, x2, y2));
return dp[x1][y1][x2][y2];
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
#endif // SOMETHING
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
scanf("%d", &a[i][j]);
}
pre();
printf("%d\n", rec(1, 1, n, m));
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |