紫书226
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <deque>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <iomanip>
using namespace std;
#define rep(i, a, b) for( i = (a); i <= (b); i++)
#define reps(i, a, b) for( i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define CLR(x,t) memset(x,t,sizeof x)
#define LEN(X) strlen(X)
#define F first
#define S second
#define Debug(x) cout<<#x<<"="<<x<<endl;
const double euler_r = 0.57721566490153286060651209;
const double pi = 3.141592653589793238462643383279;
const double E = 2.7182818284590452353602874713526;
const int inf=~0U>>1;
const int MOD = int(1e9) + 7;
const double EPS=1e-6;
typedef long long LL;
const int M = 100010;
int a[M], t[M], n;
LL cnt;//可能超int
void out()
{
for(int i = 0; i < n; i++) cout << a[i] << " ";
cout << endl;
cout << cnt << endl;
}
void merge_sort(int x, int y)//[x, y)
{
if(y - x > 1)
{
int m = x + (y - x) / 2;
int p = x, q = m, i = x;
merge_sort(x, m);
merge_sort(m, y);
while(p < m || q < y)
{
if(q >= y || (p < m && a[p] <= a[q])) t[i++] = a[p++];
else {t[i++] = a[q++]; cnt += m - p;}
}
for(i = x; i < y; i++) a[i] = t[i];
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
//归并排序 && 求逆序数
//归并排序是稳定排序,其算法复杂度是 nlogn 的
while(cin >> n)
{
cnt = 0;
for(int i = 0; i < n; i++ ) cin >> a[i];
merge_sort(0, n); //对a数组的[0, n)排序
out();
}
return 0;
}