#include    <iostream>
#include    <vector>

using namespace std;

void printVector(const vector<int> &A) {
    for(int i = 0; i < A.size(); i++) {
        cout << A[i];
        if(i+1 < A.size()) cout << ' ';
    }
    cout << endl;
}

int main() {
    int n, count = 0;
    auto swap = [](int &a, int &b) {
        int tmp = a;
        a = b;
        b = tmp;
    };

    cin >> n;
    vector<int> A(n);
    for(int i = 0; i < n; i++) {
        cin >> A[i];
    }
    
    for(int i = 0; i < n-1; i++) {
        for(int j = n-2; j >= i; j--) {
            if(A[j] > A[j+1]) {
                swap(A[j], A[j+1]);
                count++;
            }
        }
    }
    printVector(A);
    cout << count << endl;

    return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <set>
#include <map>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

int main(int argc,char** argv) {
	int n,t;
	vector<int> a;
	cin >> n;
	for(int i = 0;i < n;i++) {
		cin >> t;
		a.push_back(t);
	}
	bool flag = true;
	int count = 0;
	while(flag) {
		flag = false;
		for(auto itr = a.begin() + 1;itr != a.end();itr++) {
			if(*itr < *(itr - 1)) {
				iter_swap(itr,itr-1);
				flag = true;
				count++;
			}
		}
	}
	for(int i = 0;i < a.size()-1;i++) cout << a[i] << " ";
	cout << a[a.size()-1] << endl << count << endl;
}

