#include #pragma GCC optimize("Ofast") #pragma GCC oprimize("unrol-loops") #define vec vector #define fi first #define se second #define all(x) (x).begin(), (x).end() #define int long long using namespace std; typedef pair pii; const int inf = 2e18; int mod = 100000007; void solve() { int ans = 0; int n; cin>>n; vec a; vec kol(30, 0); for(int i = 0; i>x; if(x != 2 && x!=4 && x != 8 && x != 16 && x != 3 && x != 9) { if(kol[x]==0) a.push_back(x); else ans+=x; } kol[x]++; } bool flag = false; int ind = 1; int max_dv = 0; int max_tr = 0; for(int p=2; p<=20; p*=2) { if(kol[p]) { if(flag) ans+=ind; if(!flag) { flag = true; a.push_back(p); } ans+=(kol[p]-1)*p; ind = p; max_dv=p; } } flag = false; ind = 1; for(int p=3; p<=20; p*=3) { if(kol[p]) { if(flag) ans+=ind; if(!flag) { flag = true; a.push_back(p); } ans+=(kol[p]-1)*p; ind = p; max_tr = p; } } vec b; for(int i = 0; i ans2; do { int pl = 0; vec c = b; if(b[0] == 2) { c[0] = max_dv; } if(b[b.size()-1] == 2) { c[b.size()-1] = max_dv; } if(b[0] == 3) { c[0] = max_tr; } if(b[b.size()-1] == 3) { c[b.size()-1] = max_tr; } for(int i = 0; i ma) { ma = pl; ans2 = b; for(int j = 0; j