Pagini recente » Cod sursa (job #1603733) | Cod sursa (job #1710335) | Cod sursa (job #1079367) | Cod sursa (job #2837660) | Cod sursa (job #7294)
Cod sursa(job #7294)
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define FIN "elimin.in"
#define FOUT "elimin.out"
#define A(x,y) *(A+(x-1)*M+y)
#define Sr(x) *(Sr+x)
#define Sc(y) *(Sc+y)
#define St(x) *(St+x)
long *A;
long N, M, max, S, Nr, Nc, R, C;
long *Sr, *Sc;
long *St;
void read_data() {
long i, x, j;
freopen(FIN, "r", stdin);
scanf("%ld %ld %ld %ld", &N, &M, &R, &C);
Sr = (long*)malloc(sizeof(long) * N);
Sc = (long*)malloc(sizeof(long) * M);
A = (long*)malloc(sizeof(long) * N*M);
for (i=0; i<N; ++i)
for (j=0; j<M; ++j) {
scanf("%ld", &x);
A(i,j) = x;
Sr(i) += x;
Sc(j) += x;
}
for (S=0, i=0; i<M; ++i)
S+=Sc[i];
fclose(stdin);
}
void eval() {
long tmp = S, i,j;
for (i=0; i<N; ++i)
if ( St(i) ) {
tmp-= Sr(i);
if (tmp<max) return;
}
for (j=0; j<M; ++j)
if ( St(N+j) ) {
tmp-= Sc(j);
if (tmp<max) return;
}
for (i=0; i<N; ++i)
if ( St(i) )
for (j=0; j<M; ++j)
if (St(j))
tmp+=A(i,j);
if (tmp>max)
max = tmp;
}
void back(long x) {
if (Nr==R && x!=N+1)
back(N+1);
if (Nc==C) { eval();return; }
St(x) = 0;
if (x<N+M-1)
back(x+1);
if ( ( x<N && Nr<R ) || ( x>=N && Nc<C ) ) {
St(x) = 1;
if ( x<N ) Nr++;
else Nc++;
if (x<N+M-1)
back(x+1);
}
}
void write_data() {
freopen(FOUT, "w", stdout);
printf("%ld\n", max);
fclose(stdout);
}
int main() {
read_data();
St = (long*)malloc(sizeof(long)*(N+M));
Nr = 0; Nc = 0;
back(0);
write_data();
return 0;
}