제출 #798090

#제출 시각아이디문제언어결과실행 시간메모리
798090AkramElOmrani건포도 (IOI09_raisins)C++17
100 / 100
180 ms34532 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define int ll
#define ios ios_base::sync_with_stdio(0);cin.tie(0);
 
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
 
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int gen(int a, int b) {return (ll)rng() % (b - a + 1) + a;}
 
const int INF = 1e18L;

const int nax = 51;
int memo[nax][nax][nax][nax];
int pref[nax][nax];
int dp(int i, int j, int ii, int jj) {
	if(make_pair(i, j) == make_pair(ii, jj)) return 0LL;
	int& p = memo[i][j][ii][jj];
	if(p != INF) return p;
	int sm = pref[ii + 1][jj + 1]  - pref[i][jj + 1] - pref[ii + 1][j] + pref[i][j];
	int mn = INF;
	for(int f = i; f < ii; ++f) {
		mn = min(mn, dp(i, j, f, jj) + dp(f + 1, j, ii, jj));
	}
	for(int f = j; f < jj; ++f) {
		mn = min(mn, dp(i, j, ii, f) + dp(i, f + 1, ii, jj));
	}
	p = mn + sm;
	return p;
}

signed main()
{
	ios
	int n, m; cin >> n >> m;
	vector<vector<int>> a(n, vector<int>(m));
	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < m; ++j) {
			cin >> a[i][j];
			pref[i + 1][j + 1] += a[i][j];
		}
	}
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= m; ++j) {
			pref[i][j] += pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
		}
	}
	
	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < m; ++j) {
			for(int ii = i; ii < n; ii++)  {
				for(int jj = j; jj < m; ++jj) {
					memo[i][j][ii][jj] = INF;
				}
			}
		}
	}
 
	
	cout << dp(0, 0, n - 1, m - 1) << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...