Pagini recente » Cod sursa (job #2475699) | Cod sursa (job #1662149) | Cod sursa (job #1950899) | Cod sursa (job #1447390) | Cod sursa (job #119265)
Cod sursa(job #119265)
#include <fstream.h>
#include <values.h>
//using namespace std;
ifstream fin("elimin.in");
ofstream fout("elimin.out");
long long max=100000000;
long n,m,l,c,auxc,col[1009],a[500][500],b[500][500];
void citire()
{
fin>>n>>m>>l>>c;
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)
fin>>a[i][j];
fin.close();
}
void suma()
{
long long S=0;
for (int k=0;k<m && k<n;k++)
if (col[k]==1)
for (int r=0;r<n && r<m;r++)
S+=a[r][k];
if (S<max)
max=S;
}
void backc(int k)
{
if (k==m)
{
if (auxc==c)
suma();
return ;
}
if (auxc<c)
{
col[k]=1;
auxc++;
backc(k+1);
col[k]=0;
auxc--;
backc(k+1);
}
else
backc(k+1);
}
void sortare()
{
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)
a[i][m]+=a[i][j];
short ok=1,p=n;
while (ok)
{
ok=0;
p--;
for (int y=0;y<p;y++)
if (a[y][m]>a[y+1][m])
{
for (int pp=0;pp<m+1;pp++)
{
int aux=a[y][pp];
a[y][pp]=a[y+1][pp];
a[y+1][pp]=aux;
}
ok=1;
}
}
}
void eliminare()
{
for (int i=0;i<l;i++)
for (int j=0;j<m;j++)
a[i][j]=0;
}
void inversare()
{
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)
b[i][j]=a[i][j];
for (int y=0;y<m;y++)
for (int k=0;k<n;k++)
a[y][k]=b[k][m-1-y];
int u=n;
n=m;
m=u;
}
int main()
{
citire();
if (l>=c)
{
sortare();
eliminare();
}
else
{
int ass=l;
l=c;
c=ass;
inversare();
sortare();
eliminare();
}
backc(0);
long long Su=0;
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)
Su+=a[i][j];
Su-=max;
fout<<Su<<"\n";
fout.close();
return 0;
}