Submission #799622

#TimeUsernameProblemLanguageResultExecution timeMemory
799622I_Love_EliskaM_Raisins (IOI09_raisins)C++14
100 / 100
222 ms82636 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define forn(i,n) for(int i=0;i<n;++i)
#define pi pair<int,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vii(a,n) vector<int>a(n);forn(i,n)cin>>a[i];

const int inf = 1e15;

const int N=50;
int dp[N][N][N][N];
int mem[N][N][N][N];
int pr[N+1][N+1];

int query(int a, int b, int c, int d) {
	a++, b++, c++, d++;
	return pr[c][d] - pr[c][b-1] - pr[a-1][d] + pr[a-1][b-1];
}

void rec(int a, int b, int c, int d) {
	if (mem[a][b][c][d]) return;
	mem[a][b][c][d]=1;
	for (int m=a; m<c; ++m) {
		rec(a,b,m,d); rec(m+1,b,c,d);
		dp[a][b][c][d]=min(dp[a][b][c][d],dp[a][b][m][d]+dp[m+1][b][c][d]);
	}
	for (int m=b; m<d; ++m) {
		rec(a,b,c,m); rec(a,m+1,c,d);
		dp[a][b][c][d]=min(dp[a][b][c][d],dp[a][b][c][m]+dp[a][m+1][c][d]);
	}
	dp[a][b][c][d]+=query(a,b,c,d);
	//cout<<a<<' '<<b<<' '<<c<<' '<<d<<"  "<<dp[a][b][c][d]<<'\n';
}

void solve() {

	int n,m; cin>>n>>m;
	forn(i,n) forn(j,m) forn(k,n) forn(t,m) dp[i][j][k][t]=inf;
	forn(i,n) forn(j,m) dp[i][j][i][j]=0;
	forn(i,n) forn(j,m) mem[i][j][i][j]=1;
	forn(i,n) {
		forn(j,m) {
			int x; cin>>x;
			pr[i+1][j+1]=pr[i+1][j]+pr[i][j+1]-pr[i][j]+x;
			//cout<<pr[i+1][j+1]<<' ';
		}
		//cout<<'\n';
	}
	rec(0,0,n-1,m-1);
	cout<<dp[0][0][n-1][m-1]<<'\n';

}

int32_t main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...