Submission #585646

# Submission time Handle Problem Language Result Execution time Memory
585646 2022-06-29T07:14:50 Z Drew_ Raisins (IOI09_raisins) C++17
100 / 100
752 ms 27236 KB
#include <bits/stdc++.h>
using namespace std;

const int MAX = 69;
const int inf = 1e9 + 69;

int N, M;
int pfx[MAX][MAX];
int dp[MAX][MAX][MAX][MAX];

inline int sum(int r1, int c1, int r2, int c2)
{
	return pfx[r2][c2] - pfx[r1-1][c2] - pfx[r2][c1-1] + pfx[r1-1][c1-1];
}

inline int solve(int r1, int c1, int r2, int c2)
{
	if (r1 == r2 && c1 == c2)
		return 0;
	if (dp[r1][c1][r2][c2] != inf)
		return dp[r1][c1][r2][c2];

	for (int i = r1; i < r2; ++i)
		dp[r1][c1][r2][c2] = min(dp[r1][c1][r2][c2], sum(r1, c1, r2, c2) + solve(r1, c1, i, c2) + solve(i+1, c1, r2, c2));
	for (int i = c1; i < c2; ++i)
		dp[r1][c1][r2][c2] = min(dp[r1][c1][r2][c2], sum(r1, c1, r2, c2) + solve(r1, c1, r2, i) + solve(r1, i+1, r2, c2));
	return dp[r1][c1][r2][c2];
}

int main()
{
	ios :: sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> N >> M;
	for (int i = 1; i <= N; ++i)
		for (int j = 1; j <= M; ++j)
			cin >> pfx[i][j], pfx[i][j] += pfx[i-1][j] + pfx[i][j-1] - pfx[i-1][j-1];

	for (int i = 1; i <= N; ++i)
		for (int j = 1; j <= M; ++j)
			for (int k = i; k <= N; ++k)
				for (int l = j; l <= M; ++l)
					dp[i][j][k][l] = inf;

	cout << solve(1, 1, N, M) << '\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 468 KB Output is correct
6 Correct 2 ms 852 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
8 Correct 9 ms 2900 KB Output is correct
9 Correct 15 ms 4184 KB Output is correct
10 Correct 23 ms 4948 KB Output is correct
11 Correct 19 ms 4172 KB Output is correct
12 Correct 69 ms 8532 KB Output is correct
13 Correct 132 ms 11028 KB Output is correct
14 Correct 34 ms 5216 KB Output is correct
15 Correct 137 ms 12300 KB Output is correct
16 Correct 12 ms 4308 KB Output is correct
17 Correct 56 ms 8884 KB Output is correct
18 Correct 405 ms 20300 KB Output is correct
19 Correct 577 ms 25004 KB Output is correct
20 Correct 752 ms 27236 KB Output is correct