Pagini recente » Cod sursa (job #1142760) | Cod sursa (job #2401274) | Cod sursa (job #2390780) | Cod sursa (job #2150933) | Cod sursa (job #1147966)
#include <cstdio>
#include <algorithm>
#define INF 2000000000
#define Nmax 1005
using namespace std;
int a[Nmax][Nmax],dg[Nmax][Nmax],l[Nmax][Nmax],c[Nmax][Nmax],dp[Nmax][Nmax];
int main()
{
int i,j,s=0,N,K,sol=INF;
freopen ("ferma2.in","r",stdin);
freopen ("ferma2.out","w",stdout);
scanf("%d%d", &N,&K);
for(i=1;i<=N;++i)
for(j=1;j<=i;++j)
{
scanf("%d", &a[i][j]);
s+=a[i][j];
}
K=N-K;
for(i=1;i<=N;++i)
for(j=1;j<=N;++j)
{
l[i][j]=l[i][j-1]+a[i][j];
c[i][j]=c[i-1][j]+a[i][j];
dg[i][j]=dg[i-1][j-1]+a[i][j];
}
for(i=1;i<=K;++i)
for(j=1;j<=i;++j)
dp[K][K]+=a[i][j];
for(j=K+1;j<=N;++j)
dp[K][j]=dp[K][j-1]+dg[K][j]-c[K][j-K];
for(i=K+1;i<=N;++i)
for(j=K;j<=N;++j)
dp[i][j]=dp[i-1][j]-(dg[i-1][j]-dg[i-K-1][j-K])+(l[i][j]-l[i][j-K]);
for(i=K;i<=N;++i)
for(j=K;j<=i;++j)
sol=min(sol,dp[i][j]);
printf("%d\n", s-sol);
return 0;
}