# Cod sursa(job #1783571)

Utilizator Data 19 octombrie 2016 09:33:49 Ferma2 100 cpp done Arhiva de probleme 1.19 kb
``````#include <bits/stdc++.h>
#define Nmax 1005

using namespace std;

int n, k, S;
int sl[Nmax][Nmax];
int sc[Nmax][Nmax];
int sd[Nmax][Nmax];

void Citire()
{
ifstream f("ferma2.in");
f >> n >> k;
int i, j, pret;
for(i = 1; i <= n; i++)
for(j = 1; j <= i; j++)
{
f >> pret;
S += pret;
sl[i][j] = sl[i][j - 1] + pret;
sc[i][j] = sc[i - 1][j] + pret;
sd[i][j] = sd[i - 1][j - 1] + pret;
}
f.close();
}

void Rezolv()
{
int lat, i, j, sum, s1, smin;
lat = n - k;
sum = 0;
for(i = 1; i <= lat; i++)
sum += sl[i][i];
smin = s1 = sum;
for(i = 2; i <= k + 1; i++)
{
s1 = sum = s1 + sl[i + lat - 1][lat] - sd[i + lat - 2][lat];
smin = min(smin, sum);
for(j = 2; j <= i; j++)
{
sum = sum - (sc[i + lat - 1][j - 1] - sc[i - 1][j - 1]) +
(sd[i + lat - 1][j + lat - 1] - sd[i - 1][j - 1]);
smin = min(smin, sum);
}
}
ofstream g("ferma2.out");
g << S - smin << "\n";
g.close();
}

int main()
{
Citire();
Rezolv();
return 0;
}
``````