# 【DP~最大子立方体】吃西瓜

吃西瓜[matrix.pas/c/cpp]

[说明]此题中出现的所有数全为整数

[背景]SubRaY有一天得到一块西瓜,是长方体形的....

[题目描述]SubRaY发现这块西瓜长m厘米,宽n厘米,高h厘米.他发现如果把这块西瓜平均地分成m*n*h块1立方厘米的小正方体,那么每一小块都会有一个营养值(可能为负,因为西瓜是有可能坏掉的,但是绝对值不超过200).

[输入][matrix.in]

[输出][matrix.out]

SubRaY所能得到的最大营养值

[样例输入]

2 3 4

4 1 2 8

0 5 -48 4

3 0 1 9

2 1 4 9

1 0 1 7

3 1 2 8

[样例输出]

45

[数据范围]

=====================================

============================

{
ID:jie19952
PROG:
LANG:PASCAL
}
var
h,m,n:longint;
sum,map:array[0..50,0..50,0..50]of longint;
sum1:array[0..50,0..50]of longint;
sum2:array[0..50]of longint;
procedure init;
begin
assign(input,'matrix.in');
assign(output,'matrix.out');
reset(input); rewrite(output);
end;

procedure terminate;
begin
close(input); close(output);
halt;
end;

function max(a,b:longint):longint;
begin
if a>b then exit(a);
exit(b);
end;

procedure main;
var
h1,m1,m2,n1,n2:longint;
ans:longint;
begin
fillchar(map,sizeof(map),0);
fillchar(sum,sizeof(sum),0);

for h1:=1 to h do
for m1:=1 to m do
for n1:=1 to n do
begin
sum[h1,m1,n1]:=sum[h1,m1,n1-1]+map[h1,m1,n1];
end;

ans:=-maxlongint;
For n1:=1 to n do
for n2:=n1 to n do
begin
//fillchar(sum1,sizeof(sum1),0);
for h1:=1 to h do
begin
sum1[h1,0]:=0;
for m1:=1 to m do
begin
sum1[h1,m1]:=sum1[h1,m1-1]+sum[h1,m1,n2]-sum[h1,m1,n1-1];
end;
end;
sum2[0]:=0;
for m1:=1 to m do
for m2:=m1 to m do
begin
for h1:=1 to h do
begin
sum2[h1]:=max(sum2[h1-1]+sum1[h1,m2]-sum1[h1,m1-1],sum1[h1,m2]-sum1[h1,m1-1]);
if sum2[h1]>ans then ans:=sum2[h1];
end;
end;
end;
writeln(ans);
end;

begin
init;
main;
terminate;
end.