#include<iostream>
#include<string>
#include<cmath>
#include<list>
#include<algorithm>
using namespace std;
int a[205];
int t[205];
int n = 0,ans;
void mergeSort(int* a,int x,int y,int* t){
if(y-x<=1) return;
int m = x + (y-x)/2;
int p = x,q = m,i=x;
mergeSort(a,x,m,t);
mergeSort(a,m,y,t);
while(p<m||q<y){
if(q>=y||(p<m&&a[p]<=a[q])) t[i++] = a[p++];
else {
t[i++] = a[q++];
ans += m-p;
}
}
for(i = x;i < y;i++) a[i] = t[i];
}
void quickSort(int* a, int left,int right){
if(left>right) return;
int i = left,j = right,temp,t;
temp = a[left];
while(i!=j){
while(a[j]>=temp&&i<j) j--;
while(a[i]<=temp&&i<j) i++;
if(i<j){
t = a[j];a[j]=a[i];a[i]=t;
}
}
a[left] = a[i];
a[i] = temp;
quickSort(a,left,i-1);
quickSort(a,i+1,right);
}
void insertSort(int * a,int n){
int b[n+1];
for(int i = 1;i <= n;i++) b[i]=a[i-1];
int temp,i,j;
for(i = 2;i <= n;i++){
if(b[i]<b[i-1]){
b[0] = b[i];
for(j = i-1;b[0]<b[j];j--)
b[j+1]=b[j];
b[j+1] =b[0];
}
}
for(int i = 1;i <= n;i++) a[i-1]=b[i];
}
void swap(int x,int y){
int t = a[x];a[x] = a[y];a[y] = t;
}
void shiftDown(int x){
bool f = false;
int t=x;
while(x*2<=n&&!f){
if(a[x]<a[x*2]) t = x*2;
else t = x;
if(x*2+1<=n){
if(a[t]<a[x*2+1]) t = x*2+1;
}
if(t!=x){
swap(t,x);
x = t;
}else {
f = true;
}
}
}
void create(){
for(int i = n/2;i>=1;i--)
shiftDown(i);
}
void heapSort(){
while(n>1){
swap(1,n);
n--;
shiftDown(1);
}
}
int main(){
int n;
cin>>n;
for(int i = 1;i <= n;i++){
cin>>a[i];
}
// mergeSort(a,0,n,t);
// quickSort(a,0,n);
// insertSort(a,n);
int te = n;
create();
heapSort();
for(int it = 1;it <= te;it++){
cout<<a[it]<<" ";
}
cout<<endl;
cout<<ans;
return 0;
}