7622022-01-09 18:09:104rt3xp0Táblatöréscpp11Accepted 50/509ms6388 KiB
#include<bits/stdc++.h>
using namespace std;
int pref[32][32],dp[32][32][32][32];
int sum(int x1, int x2, int y1, int y2){
	return pref[x2][y2]+pref[x1-1][y1-1]-pref[x2][y1-1]-pref[x1-1][y2];
}

int main(){
	int n,m; cin >> n >> m;
	memset(pref,0,32*32); memset(dp,0,32*32*32*32);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin >> pref[i][j];
			pref[i][j]+=pref[i-1][j]+pref[i][j-1]-pref[i-1][j-1];
		}
	}
	for(int l=2;l<=n+m;l++){
		int lx=max(1,l-m);
		int ly=l-lx;
		while(lx>=1 && lx<=n && ly>=1 && ly<=m){
			for(int x1=1;x1+lx-1<=n;x1++){
				int x2=x1+lx-1;
				for(int y1=1;y1+ly-1<=m;y1++){
					int y2=y1+ly-1;
					if((n+m-l)%2==0) dp[x1][x2][y1][y2]=max({dp[x1+1][x2][y1][y2]+sum(x1,x1,y1,y2),dp[x1][x2-1][y1][y2]+sum(x2,x2,y1,y2), dp[x1][x2][y1+1][y2]+sum(x1,x2,y1,y1),dp[x1][x2][y1][y2-1]+sum(x1,x2,y2,y2)});
					else dp[x1][x2][y1][y2]=min({dp[x1+1][x2][y1][y2],dp[x1][x2-1][y1][y2],dp[x1][x2][y1+1][y2],dp[x1][x2][y1][y2-1]});
				}
			}
			lx++; ly--;
		}
	}
	cout << dp[1][n][1][m];
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms3840 KiB
2Accepted0/09ms6156 KiB
3Accepted2/22ms3972 KiB
4Accepted2/22ms3976 KiB
5Accepted1/12ms3984 KiB
6Accepted1/13ms4004 KiB
7Accepted1/14ms6116 KiB
8Accepted2/23ms4852 KiB
9Accepted3/32ms4080 KiB
10Accepted3/33ms4516 KiB
11Accepted3/34ms5528 KiB
12Accepted3/34ms5532 KiB
13Accepted4/47ms6136 KiB
14Accepted4/48ms6140 KiB
15Accepted4/48ms6364 KiB
16Accepted5/58ms6368 KiB
17Accepted5/58ms6376 KiB
18Accepted5/58ms6380 KiB
19Accepted1/18ms6384 KiB
20Accepted1/18ms6388 KiB