#include <fstream>
using namespace std;
ifstream is("date.in");
ofstream os("date.out");
int x[20],n,b[20],pmax=0;
void afis()
{
for (int i = 1; i <= n; i++) os<<b[i]<<" ";
os<<endl;
}
void alege()//alege permutarea pentru care se obtine suma maxima
{
int p=x[1]*x[n];
for(int i=1;i<n;i++)
p=p+x[i]*x[i+1];
if(p>pmax)
{
pmax=p;
for(int i=1;i<=n;i++) b[i]=x[i];
}
}
void inter(int &x, int &y)//interschimba doua valori
{
int aux=x; x=y; y=aux;
}
void perm(int k, int n)//genereaza permutarile
{
for (int i = k; i <= n; i++)
{
inter(x[k], x[i]);
if(k==n) alege();
else perm(k+1, n);
inter(x[k], x[i]);
}
}
int main()
{
is>>n;
for(int i=1;i<=n;i++) is>>x[i];
perm(1,n);
afis();
return 0;
}
|