Read post
#include <stdio.h>
#include <string.h>
#define MIN -1000000000
long max1(long *m,long n)
{
long sum=MIN,temp=MIN,i;
for (i=1;i<=n;++i)
{
if (temp>0) temp+=m[i];
else temp=m[i];
if (temp>sum) sum=temp;
}
return sum;
}
long max2(long m[][21],long n)
{
long s[21],i,j,k,sum=MIN,temp;
for (i=1;i<=n;++i)
{
memset(s,0,sizeof(s));
for (j=i;j<=n;++j)
{
for (k=1;k<=n;++k)
s[k]+=m[j][k];
temp=max1(s,n);
if (temp>sum) sum=temp;
}
}
return sum;
}
int main()
{
long m[21][21][21],s[21][21],i,j,k,l,sum=MIN,temp,n;
while (1)
{
scanf("%ld",&n);
if (!n) return 0;
for (i=1;i<=n;++i)
for (j=1;j<=n;++j)
for (k=1;k<=n;++k)
scanf("%ld",&m[i][j][k]);
for (i=1;i<=n;++i)
{
memset(s,0,sizeof(s));
for (j=i;j<=n;++j)
{
for (k=1;k<=n;++k)
for (l=1;l<=n;++l)
s[k][l]+=m[j][k][l];
temp=max2(s,n);
if (temp>sum) sum=temp;
}
}
printf("%ld\n",sum);
}
}
Reply
|