# Cod sursa(job #636155)

Utilizator Data 19 noiembrie 2011 17:27:53 Ferma2 90 cpp done .com 2011 0.98 kb
``````#include <stdio.h>
#define NMAX 1010
int a[NMAX][NMAX], n, k, i, j, l;
long sum[NMAX][NMAX], sTotal, min;
int main()
{
freopen("ferma2.in", "r", stdin);
freopen("ferma2.out", "w", stdout);
scanf("%d%d", &n, &k);
for (i=0; i<n; i++)
for (j=0; j<=i; j++)
{
scanf("%d", &a[i][j]);
sTotal+=a[i][j];
}//for j
for (i=0; i<n-k; i++)
for (j=0; j<=i; j++)
sum[0][0]+=a[i][j];
min=sum[0][0];
for (i=1; i<=(n-(n-k)); i++)
for (j=0; j<=i; j++)
{
if (sum[i-1][j]>0)
{
sum[i][j]=sum[i-1][j];
for (l=0; l<(n-k); l++)
{
sum[i][j]-=a[i-1+l][j+l];
sum[i][j]+=a[i+n-k-1][j+l];
}//for l
}//if
else
{
sum[i][j]=sum[i][j-1];
for (l=0; l<(n-k); l++)
{
sum[i][j]-=a[i+l][j-1];
sum[i][j]+=a[i+l][j+l];
}//for l
}//else
if (sum[i][j]<min)
min=sum[i][j];
}//for j
printf("%ld\n", sTotal-min);
/*	for (i=0; i<n; i++)
{
for (j=0; j<=i; j++)

printf("%ld ", sum[i][j]);
printf("\n");
}//for i*/
return 0;
}//main
``````