import std.stdio, std.string, std.array, std.conv, std.algorithm;

int bubbleSort(int n, int[] x) {
    int cnt = 0;
    bool flag = true;
    for (int i = 0; flag; ++i) {
        flag = false;
        for (int j = n - 1; j >= i + 1; --j) {
            if (x[j] < x[j-1]) {
                swap(x[j], x[j-1]);
                flag = true;
                ++cnt;
            }
        }
    }
    return cnt;
}

void main() {
    int n = readln.chomp.to!int;
    int[] a = readln.chomp.split.to!(int[]);
    int times = bubbleSort(n, a);
    foreach (i, x; a) {
        x.write;
        if (i != n - 1) " ".write;
        else writeln;
    }
    times.writeln;
}
