# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
545366 | alextodoran | Raisins (IOI09_raisins) | C++17 | 109 ms | 21424 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_MAX = 50;
int N, M;
int R[N_MAX + 2][N_MAX + 2];
int sum[N_MAX + 2][N_MAX + 2];
int minCost[N_MAX + 2][N_MAX + 2][N_MAX + 2][N_MAX + 2];
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
cin >> R[i][j];
}
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
sum[i][j] = R[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
}
}
for (int i1 = N; i1 >= 1; i1--) {
for (int i2 = i1; i2 <= N; i2++) {
for (int j1 = M; j1 >= 1; j1--) {
for (int j2 = j1; j2 <= M; j2++) {
if (i1 == i2 && j1 == j2) {
minCost[i1][j1][i2][j2] = 0;
} else {
minCost[i1][j1][i2][j2] = INT_MAX;
for (int i = i1; i < i2; i++) {
minCost[i1][j1][i2][j2] = min(minCost[i1][j1][i2][j2],
minCost[i1][j1][i][j2] + minCost[i + 1][j1][i2][j2]);
}
for (int j = j1; j < j2; j++) {
minCost[i1][j1][i2][j2] = min(minCost[i1][j1][i2][j2],
minCost[i1][j1][i2][j] + minCost[i1][j + 1][i2][j2]);
}
minCost[i1][j1][i2][j2] +=
sum[i2][j2] - sum[i1 - 1][j2] - sum[i2][j1 - 1] + sum[i1 - 1][j1 - 1];
}
}
}
}
}
cout << minCost[1][1][N][M] << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |