Cod sursa(job #119044)

Utilizator RobytzzaIonescu Robert Marius Robytzza Data 29 decembrie 2007 11:34:16
Problema Elimin Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.82 kb
#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,auxl,lin[1009],col[1009],a[200][200],b[200][200];

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++)
      if (col[k]==1)
	 for (int r=0;r<n;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 p=0;p<m+1;p++)
	       {
		  int aux=a[y][p];
		  a[y][p]=a[y+1][p];
		  a[y+1][p]=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;
}