# Cod sursa(job #1625571)

Utilizator Data 2 martie 2016 19:41:12 Ferma2 100 cpp done Arhiva de probleme 1.33 kb
``````#include <bits/stdc++.h>

using namespace std;

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

const int NMax = 1e3 + 5;
const int BMax = 1e4;

int A[NMax][NMax], B[NMax][NMax];

int pos = BMax - 1;
char Buffer[BMax];
while(!isdigit(Buffer[pos])){
if(++pos == BMax){
pos = 0;
}
}
x = 0;
while(isdigit(Buffer[pos])){
x = x * 10 + (Buffer[pos] - '0');
if(++pos == BMax){
pos = 0;
}
}
}

int main(){
int n, k, sum, l, Min, area;
sum = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= i; j++){
sum += A[i][j];
B[i][j] = A[i][j] + B[i - 1][j - 1];
A[i][j] = A[i - 1][j] + B[i][j];
}
}
for(int i = 1; i <= n; i++){
for(int j = i; j > 0; j--){
B[i][j] += B[i][j + 1];
}
}
l = n - k;
Min = B[l][1];
for(int i = l + 1; i <= n; i++){
for(int j = 1; j <= i - l + 1; j++){
area = B[i][j] - B[i][j + l] - A[i - 1][j - 1] + A[i - l - 1][j - 1];
Min = min(Min, area);
}
}
fout << sum - Min;
return 0;
}
``````