Cod sursa(job #18288)

Utilizator mirceacnula la ala mirceacnu Data 18 februarie 2007 11:14:39
Problema Tricouri Scor 0
Compilator cpp Status done
Runda preONI 2007, Runda 2, Clasa a 9-a si gimnaziu Marime 0.99 kb
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
int a[300],n,m,k,p,v[5],i,j,x,b;
int main()
{
      ifstream f("tricouri.in");
      ofstream q("tricouri.out");
      f>>n;f>>m;
      for (i=1;i<=n;i++)f>>a[i];
      b=0;
      while (b!=m)
	       {f>>k; f>>p; v[1]=0;v[2]=0;v[3]=0;v[4]=0;v[5]=0;
			for (j=1;j<=n;j++)
					  if (a[j]%p==0)
					  if (a[j]>v[1]){
							for (x=k;x>1;x--)
							v[x]=v[x-1];
							v[1]=a[j];
							}
							else if (a[j]>v[2])
							{
							for (x=k;x>2;x--)
							v[x]=v[x-1];
							v[2]=a[j];}
							else if (a[j]>v[3])
							{
							for (x=k;x>3;x--)
							v[x]=v[x-1];
							v[3]=a[j];}
							else if (a[j]>v[4])
							{
							for (x=k;x>4;x--)
							v[x]=v[x-1];
							v[4]=a[j];}
							else if (a[j]>v[5])
							{
							v[5]=a[j];}
			       if (v[1]==0) q<<"-1"<<endl; else q<<v[1]+v[2]+v[3]+v[4]+v[5]<<endl;
			       b++;
			       }

      f.close();
      q.close();
      return 0;
}