# Cod sursa(job #2037066)

Utilizator Data 11 octombrie 2017 17:42:05 Ferma2 100 cpp done Arhiva de probleme 1.02 kb
``````#include <iostream>
#include <fstream>

using namespace std;
ifstream fin("ferma2.in");
ofstream fout("ferma2.out");

int v[1003][1003], s[1003][1003], c[1003][1003], l[1003][1003], d[1003][1003];
int n, k, j, i, sum, mn, sM;

int main () {

fin >> n >> k;
k = n-k;
for (i = 1; i <= n; i++)
for (j = 1; j <= i; j++)
{
fin >> v[i][j];
c[i][j] = v[i][j];
d[i][j] = v[i][j];
l[i][j] = v[i][j];
}

for (i = 1; i <= n; i++)
for (j = 1; j <= i; j++)
{
l[i][j] += l[i][j-1];
c[i][j] += c[i-1][j];
d[i][j] += d[i-1][j-1];
if (i <= k)	sum += v[i][j];
sM += v[i][j];
}
s[k][k] = sum;

mn = sM;
for (i = 1; i <= n; i++)
for (j = 1; j <= i; j++)
{
if (s[i-1][j] != 0)
{
s[i][j] += s[i-1][j];
s[i][j] += l[i][j]-l[i][j-k];
s[i][j] -= d[i-1][j]-d[i-k-1][j-k];
}
else if (s[i][j-1] != 0)
{
s[i][j] += s[i][j-1];
s[i][j] += d[i][j]-d[i-k][j-k];
s[i][j] -= c[i][j-k]-c[i-k][j-k];
}
if (j >= k && i >= k && s[i][j] < mn)	mn = s[i][j];
}

fout << sM-mn << "\n";
}
``````