Cod sursa(job #1625571)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 2 martie 2016 19:41:12
Problema Ferma2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 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];
inline void Read(int &x){
    while(!isdigit(Buffer[pos])){
        if(++pos == BMax){
            fin.read(Buffer, BMax);
            pos = 0;
        }
    }
    x = 0;
    while(isdigit(Buffer[pos])){
        x = x * 10 + (Buffer[pos] - '0');
        if(++pos == BMax){
            fin.read(Buffer, BMax);
            pos = 0;
        }
    }
}

int main(){
    int n, k, sum, l, Min, area;
    sum = 0;
    Read(n); Read(k);
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= i; j++){
            Read(A[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;
}