# Cod sursa(job #1777502)

Utilizator Data 12 octombrie 2016 16:18:03 Ferma2 100 cpp done Arhiva de probleme 1.37 kb
``````#include <cstdio>
using namespace std;

const int NMAX = 1000;
int d[NMAX + 5][NMAX + 5];
int lin[NMAX + 5][NMAX + 5], col[NMAX + 5][NMAX + 5];
int main()
{
freopen("ferma2.in", "r", stdin);
freopen("ferma2.out", "w", stdout);

int n, k, ans, x, st;
scanf("%d %d", &n, &k);
k = n - k;

ans = 2000000000; st = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= i; ++j)
{
scanf("%d", &x); st += x;
lin[i][j] = lin[i][j - 1] + x;
col[i][j] = col[i - 1][j] + x;
}

for (int i = 1; i <= n; ++i)
for (int j = 1; j <= i; ++j)
{
int i1, j1, i2, j2;/// ca sa nu accesam elem negativi ///

//d[i][j] -= (col[i - 1][j - k] - col[i - k - 1][j - k]); //
i1 = i - 1;
j1 = j - k; if (j1 < 0) j1 = 0;
i2 = i - k - 1; if (i2 < 0) i2 = 0;
j2 = j1;
d[i][j] -= col[i1][j1] - col[i2][j2];

//d[i][j] += (lin[i][j] - lin[i][j - k]) + d[i - 1][j - 1]; //
i1 = i;
j1 = j;
i2 = i;
j2 = j - k; if (j2 < 0) j2 = 0;
d[i][j] += lin[i1][j1] - lin[i2][j2] + d[i - 1][j - 1];

if (i >= k && j >= k && d[i][j] < ans)
ans = d[i][j];
}

printf("%d\n", st - ans);
return 0;
}
``````