Cod sursa(job #2062635)

Utilizator Petru00Dascalu Octavian Petrut Petru00 Data 10 noiembrie 2017 17:41:57
Problema Ferma2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <bits/stdc++.h>
#define N 1001
using namespace std;
ifstream fin("ferma2.in");
ofstream fout("ferma2.out");
int sl[N][N],sc[N][N],sd[N][N];
int n,k,L,S,s,s1,smin;

int main()
{
    int i,j,x;
    fin >> n >> k;
    L=n-k;
    for (i=1; i<=n; ++i)
        for (j=1; j<=i; ++j)
        {
            fin>>x;
            S+=x;
            sl[i][j]=x+sl[i][j-1];
            sc[i][j]=x+sc[i-1][j];
            sd[i][j]=x+sd[i-1][j-1];
        }
    for (i=1; i<=L; ++i)s+=sl[i][i];
    smin=s1=s;
    for (i=2; i<=n-L+1; i++)
    {
        s1=s=s1+sl[i+L-1][L]-sd[i+L-2][L];
        smin=min(smin,s);
        for (j=2; j<=i; ++j)
        {
            s=s-(sc[i+L-1][j-1]-sc[i-1][j-1])+(sd[i+L-1][j+L-1]-sd[i-1][j-1]);
            smin=min(smin,s);
        }
    }
    smin=S-smin;
    fout<<smin<<"\n";
    return 0;
}