Submission #655598

#TimeUsernameProblemLanguageResultExecution timeMemory
655598horiseunRaisins (IOI09_raisins)C++11
100 / 100
695 ms30316 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
 
int n, m;
int choc[55][55];
int pref[55][55];
bool seen[55][55][55][55];
int cache[55][55][55][55];
 
int dp(int x1, int y1, int x2, int y2) {
	if (x1 == x2 && y1 == y2) return 0;
	if (seen[x1][y1][x2][y2]) return cache[x1][y1][x2][y2];
	
	cache[x1][y1][x2][y2] = INT_MAX;
	seen[x1][y1][x2][y2] = true;
	for (int i = x1; i < x2; i++) {
		cache[x1][y1][x2][y2] = min(cache[x1][y1][x2][y2], dp(x1, y1, i, y2) + dp(i + 1, y1, x2, y2));
	}
	for (int i = y1; i < y2; i++) {
		cache[x1][y1][x2][y2] = min(cache[x1][y1][x2][y2], dp(x1, y1, x2, i) + dp(x1, i + 1, x2, y2));
	}
	cache[x1][y1][x2][y2] += (pref[x2][y2] - pref[x1 - 1][y2] - pref[x2][y1 - 1] + pref[x1 - 1][y1 - 1]);
	
	return cache[x1][y1][x2][y2];
 
}
 
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 >> choc[i][j];
		}
	}
 
	pref[1][1] = choc[1][1];
	for (int i = 2; i <= m; i++) {
		pref[1][i] = pref[1][i - 1] + choc[1][i];
	}
	for (int i = 2; i <= n; i++) {
		pref[i][1] = pref[i - 1][1] + choc[i][1];
		for (int j = 2; j <= m; j++) {
			pref[i][j] = pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1] + choc[i][j];
		}
	}
 
	cout << dp(1, 1, n, m) << "\n";
 
}
#Verdict Execution timeMemoryGrader output
Fetching results...