Cod sursa(job #1008583)

Utilizator brainwashed20Alexandru Gherghe brainwashed20 Data 11 octombrie 2013 12:49:07
Problema Elimin Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include<fstream>
#include<algorithm>

using namespace std;

ifstream f("elimin.in");
ofstream g("elimin.out");

#define DIM 700

int N, M, R, C, maxsum;
int aux[DIM], sum[DIM], mat[DIM][DIM];

int main() {

    int i, j, k, s, nrb;

    f >> N >> M >> R >> C;

    for(i=1; i<=N; ++i)
        for(j=1; j<=M; ++j)
            if(N > M)
                f >> mat[j][i];
            else
                f >> mat[i][j];

    if(N > M) {
        swap(N, M);
        swap(R, C);
    }

    for(i=1; i<=N; ++i)
        for(j=1; j<=M; ++j)
            sum[j] += mat[i][j];

    for(i=0; i<(1<<N); ++i) {
        nrb = 0;
        for(j=0; (1<<j)<=i; ++j)
            if((1<<j) & i)
                ++nrb;
        if(nrb != R)
            continue;
        copy(sum+1, sum+M+1, aux+1);
        for(j=0; (1<<j)<=i; ++j)
            if((1<<j) & i)
                for(k=1; k<=M; ++k)
                    aux[k] -= mat[j+1][k];
        nth_element(aux+1, aux+C+1, aux+M+1);
        s = 0;
        for(j=C+1; j<=M; ++j)
            s += aux[j];
        maxsum = max(maxsum, s);
    }

    g << maxsum;

    f.close();
    g.close();

    return 0;
}