Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- int v[10];
- int main()
- {
- int n;
- ifstream fin("poli.txt");
- fin>>n;
- fin.ignore();
- for(int i=1; i<=n; i++)
- {
- int x;
- fin>>x;
- v[x]++;
- }
- int nr=0;
- for(int i=0; i<=9; i++)
- {
- if(v[i]%2==1)
- {
- nr++;
- }
- }
- int k=8;
- if(nr>=2)
- {
- cout<<-1;
- }
- else
- {
- bool ok=true;
- while(ok && k)
- {
- if(v[k]>=2)
- {
- cout<<k;
- v[k]--;
- ok=false;
- }
- else
- {
- k=k-2;
- }
- }
- if(!k)
- {
- cout<<-1;
- }
- else
- {
- nr=-1;
- for(int i=9; i>=0; i--)
- {
- if(v[i]%2==1 && i!=k)
- {
- for(int j=1; j<=v[i]/2; j++)
- {
- cout<<i;
- }
- nr=i;
- v[i]=v[i]-(v[i]/2)-1;
- }
- else
- {
- for(int j=1; j<=v[i]/2; j++)
- {
- cout<<i;
- }
- v[i]=v[i]-(v[i]/2);
- }
- }
- if(nr!=-1)
- {
- cout<<nr;
- }
- for(int i=0; i<=9; i++)
- {
- if(v[i]!=1 || i!=k)
- {
- for(int j=1; j<=v[i]; j++)
- {
- cout<<i;
- }
- }
- }
- }
- }
- cout<<k;
- fin.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement