def bubbleSort(A, N):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for j in xrange(N-1,0,-1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag += 1
                count += 1
    return A, count

def newbubbleSort(A, N):
    flag = 1
    count = 0
    i = 0
    while flag:
        flag = 0
        for j in xrange(N-1,i,-1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag += 1
                count += 1
        i += 1
    return A, count


def main():
    N = int(raw_input())
    A = map(int, raw_input().split())
    new_A, count = newbubbleSort(A, N)
    print ' '.join(map(str,new_A))
    print count

if __name__ == "__main__":
    main() def bubbleSort(A, N):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for j in xrange(N-1,0,-1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag += 1
                count += 1
    return A, count bubbleSort flag = 1 flag 1 count = 0 count 0 while flag:
        flag = 0
        for j in xrange(N-1,0,-1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag += 1
                count += 1
     flag flag = 0 flag 0 for j in xrange(N-1,0,-1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag += 1
                count += 1
     j xrange(N-1,0,-1) xrange N-1 N 1 0 -1 if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag += 1
                count += 1
     A[j] < A[j-1] A[j] A j A[j-1] A j-1 j 1 tmp = A[j] tmp A[j] A j A[j] = A[j-1] A[j] A j A[j-1] A j-1 j 1 A[j-1] = tmp A[j-1] A j-1 j 1 tmp flag += 1 flag 1 count += 1 count 1 return A, count return A, count A count A A N N def newbubbleSort(A, N):
    flag = 1
    count = 0
    i = 0
    while flag:
        flag = 0
        for j in xrange(N-1,i,-1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag += 1
                count += 1
        i += 1
    return A, count newbubbleSort flag = 1 flag 1 count = 0 count 0 i = 0 i 0 while flag:
        flag = 0
        for j in xrange(N-1,i,-1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag += 1
                count += 1
        i += 1
     flag flag = 0 flag 0 for j in xrange(N-1,i,-1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag += 1
                count += 1
         j xrange(N-1,i,-1) xrange N-1 N 1 i -1 if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag += 1
                count += 1
         A[j] < A[j-1] A[j] A j A[j-1] A j-1 j 1 tmp = A[j] tmp A[j] A j A[j] = A[j-1] A[j] A j A[j-1] A j-1 j 1 A[j-1] = tmp A[j-1] A j-1 j 1 tmp flag += 1 flag 1 count += 1 count 1 i += 1 i 1 return A, count return A, count A count A A N N def main():
    N = int(raw_input())
    A = map(int, raw_input().split())
    new_A, count = newbubbleSort(A, N)
    print ' '.join(map(str,new_A))
    print count main N = int(raw_input()) N int(raw_input()) int raw_input() raw_input A = map(int, raw_input().split()) A map(int, raw_input().split()) map int raw_input().split() ().split () raw_input split new_A, count = newbubbleSort(A, N) new_A newbubbleSort(A, N) newbubbleSort A N count print ' '.join(map(str,new_A)) ' '.join ' ' join map(str,new_A) map str new_A print if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def main():
    N = int(raw_input())
    A = map(int, raw_input().split())
    new_A, count = newbubbleSort(A, N)
    print ' '.join(map(str,new_A))
    print count def main():
    N = int(raw_input())
    A = map(int, raw_input().split())
    new_A, count = newbubbleSort(A, N)
    print ' '.join(map(str,new_A))
    print count main def bubbleSort(A, N):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for j in xrange(N-1,0,-1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag += 1
                count += 1
    return A, count def bubbleSort(A, N):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for j in xrange(N-1,0,-1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag += 1
                count += 1
    return A, count bubbleSort def newbubbleSort(A, N):
    flag = 1
    count = 0
    i = 0
    while flag:
        flag = 0
        for j in xrange(N-1,i,-1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag += 1
                count += 1
        i += 1
    return A, count def newbubbleSort(A, N):
    flag = 1
    count = 0
    i = 0
    while flag:
        flag = 0
        for j in xrange(N-1,i,-1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag += 1
                count += 1
        i += 1
    return A, count newbubbleSort
def bubbleSort(A, N):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for j in xrange(N-1,0,-1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag += 1
                count += 1
    return A, count

def newbubbleSort(A, N):
    flag = 1
    count = 0
    i = 0
    while flag:
        flag = 0
        for j in xrange(N-1,i,-1):
            if A[j] < A[j-1]:
                tmp = A[j]
                A[j] = A[j-1]
                A[j-1] = tmp
                flag += 1
                count += 1
        i += 1
    return A, count


def main():
    N = int(raw_input())
    A = map(int, raw_input().split())
    new_A, count = newbubbleSort(A, N)
    print ' '.join(map(str,new_A))
    print count

if __name__ == "__main__":
    main()