This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#include <ext/rope>
using namespace __gnu_cxx;
typedef tree<long long, null_type, less<long long>,
rb_tree_tag, tree_order_statistics_node_update> pbds;
//less_equal for identical elements
#define DEBUG
#ifdef DEBUG
#define debug(...) printf(__VA_ARGS__);
#else
#define debug(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb emplace_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
mt19937 rng(time(0));
#define maxn 131075
int n,x,a[maxn],cnt[18][1<<17];
ll inv[17][2];
int main(){
	sf("%d",&n);
	for(int i=0;i<(1<<n);++i){
		sf("%d",&x);
		a[x]=i;
	}
	
	if(n==0)pf("0\n"),exit(0);
	
	ll ans=LINF;int min=-1,bits=-1;
	for(int i=0;i<n;++i){
		//shifted by i
		memset(inv,0,sizeof inv);
		memset(cnt,0,sizeof cnt);
		for(int j=0;j<(1<<n);++j){
			int b=a[j];
			for(int k=0;k<n;++k){
				inv[k][b&1]+=cnt[n-k-1][b>>1]-cnt[n-k][b];
				cnt[n-k][b]++;
				b>>=1;
			}
			++cnt[0][0];
		}
		
		ll cur=0;
		int x=0;
		for(int j=0;j<n;++j){
			if(inv[j][0]<inv[j][1])cur+=inv[j][0];
			else cur+=inv[j][1],x|=(1<<j);
		}
		if(cur<ans)ans=cur,min=i,bits=x;
		
		for(int j=0;j<(1<<n);++j){
			int one=a[j]&1;
			a[j]>>=1;
			a[j]+=(one<<(n-1));
		}
	}
	
	pf("%lld\n",ans);
	for(int i=0;i<min;++i)pf("2");
	for(int i=0;i<n;++i){
		pf("2");
		if(bits&(1<<i))pf("1");
	}
	pf("\n");
}
Compilation message (stderr)
cheerleaders.cpp: In function 'int main()':
cheerleaders.cpp:48:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |  sf("%d",&n);
      |    ^
cheerleaders.cpp:50:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |   sf("%d",&x);
      |     ^| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |