제출 #276835

#제출 시각아이디문제언어결과실행 시간메모리
276835FlashGamezzz건포도 (IOI09_raisins)C++17
100 / 100
345 ms33828 KiB
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <string>
using namespace std;

int choc[50][50];
long psum[51][51] = {};
long long dp[50][50][50][50];
long sum(int r1, int c1, int r2, int c2){ //r2 > r1, c2 > c1;
	r2++;
	c2++;
	return psum[r2][c2]-psum[r2][c1]-psum[r1][c2]+psum[r1][c1];
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			cin >> choc[i][j];
		}
	}
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= m; j++){
			psum[i][j] = choc[i-1][j-1] + psum[i-1][j] + psum[i][j-1] - psum[i-1][j-1];
		}
	}
	for (int r1 = 0; r1 < n; r1++){
		for (int c1 = 0; c1 < m; c1++){
			for (int r2 = r1; r2 >= 0; r2--){
				for (int c2 = c1; c2 >= 0; c2--){
					if (r1 == r2 && c1 == c2){
						dp[r2][c2][r1][c1] = 0;
						continue;
					}
					dp[r2][c2][r1][c1] = 1000000000000000;
					for (int i = c1-1; i >= c2; i--){
						dp[r2][c2][r1][c1] = min(dp[r2][c2][r1][c1], dp[r2][c2][r1][i]+dp[r2][i+1][r1][c1]);
					}
					for (int i = r1-1; i >= r2; i--){
						dp[r2][c2][r1][c1] = min(dp[r2][c2][r1][c1], dp[r2][c2][i][c1]+dp[i+1][c2][r1][c1]);
					}
					dp[r2][c2][r1][c1] += sum(r2, c2, r1, c1);
				}
			}
		}
	}
	cout << dp[0][0][n-1][m-1] << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...