Skip to content

Instantly share code, notes, and snippets.

@StarOrpheus
Created December 14, 2015 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StarOrpheus/845e6bfe49bf1a62de26 to your computer and use it in GitHub Desktop.
Save StarOrpheus/845e6bfe49bf1a62de26 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <map>
#include <string>
#include <cstring>
//#include <unordered_map>
#include <queue>
#include <algorithm>
using namespace std;
#define INF (1<<30)
#define F first
#define S second
#define mkp(a, b) make_pair(a, b)
int s(long long x)
{
if(x < 10)
return x;
int sum = 0;
while(x)
{
sum += x % 10;
x /= 10;
}
return s(sum);
}
int main()
{
freopen("tickets.in", "r", stdin);
freopen("tickets.out", "w", stdout);
int n;
vector<long long> a;
vector< vector< long long > > mp(10);
vector<long long> ans;
cin >> n;
a.resize(n);
for(int i = 0; i < n; i++)
{
cin >> a[i];
mp[s(a[i])].push_back(a[i]);
}
while(mp[3].size() > 1)
{
ans.push_back(mp[3].back());
mp[3].pop_back();
ans[ans.size() - 1] *= mp[3].back();
mp[3].pop_back();
}
while(mp[2].size() && mp[4].size())
{
ans.push_back(mp[2].back() * mp[4].back());
mp[2].pop_back(); mp[4].pop_back();
}
while(mp[2].size() > 2)
{
ans.push_back(mp[2].back()); mp[2].pop_back();
ans[ans.size() - 1] *= mp[2].back(); mp[2].pop_back();
ans[ans.size() - 1] *= mp[2].back(); mp[2].pop_back();
}
while(mp[2].size() && mp[3].size())
{
ans.push_back(mp[2].back() * mp[3].back());
mp[2].pop_back(); mp[3].pop_back();
}
for(int i = 2; i < 10; i++)
for(int j = 0; j < mp[i].size(); j++)
ans.push_back(mp[i][j]);
sort(ans.begin(), ans.end());
cout << ans.size() << endl;
for(int i = 0; i < ans.size(); i++)
cout << ans[i] << " ";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment