def bubble_sort(A, N):
    sw = 0
    flag = True
    while flag:
        flag = False
        for j in reversed(range(1, N)):
            if  A[j - 1] > A[j]:
                A[j - 1],   A[j] = A[j], A[j - 1]
                sw += 1
                flag = True
    return sw

def main():
    N = int(input().rstrip())
    A = list(map(int, input().rstrip().split()))
     
    sw = bubble_sort(A, N)                            

    print(' '.join(map(str, A)))
    print(sw)

if __name__ == '__main__':
    main() def bubble_sort(A, N):
    sw = 0
    flag = True
    while flag:
        flag = False
        for j in reversed(range(1, N)):
            if  A[j - 1] > A[j]:
                A[j - 1],   A[j] = A[j], A[j - 1]
                sw += 1
                flag = True
    return sw bubble_sort sw = 0 sw 0 flag = True flag True while flag:
        flag = False
        for j in reversed(range(1, N)):
            if  A[j - 1] > A[j]:
                A[j - 1],   A[j] = A[j], A[j - 1]
                sw += 1
                flag = True
     flag flag = False flag False for j in reversed(range(1, N)):
            if  A[j - 1] > A[j]:
                A[j - 1],   A[j] = A[j], A[j - 1]
                sw += 1
                flag = True
     j reversed(range(1, N)) reversed range(1, N) range 1 N if  A[j - 1] > A[j]:
                A[j - 1],   A[j] = A[j], A[j - 1]
                sw += 1
                flag = True
     A[j - 1] > A[j] A[j - 1] A j - 1 j 1 A[j] A j A[j - 1],   A[j] = A[j], A[j - 1] A[j - 1] A j - 1 j 1 A[j] A j A[j] A j A[j - 1] A j - 1 j 1 sw += 1 sw 1 flag = True flag True return sw sw A A N N def main():
    N = int(input().rstrip())
    A = list(map(int, input().rstrip().split()))
     
    sw = bubble_sort(A, N)                            

    print(' '.join(map(str, A)))
    print(sw) main N = int(input().rstrip()) N int(input().rstrip()) int input().rstrip() ().rstrip () input rstrip A = list(map(int, input().rstrip().split())) A list(map(int, input().rstrip().split())) list map(int, input().rstrip().split()) map int input().rstrip().split() ().rstrip().split ().rstrip() ().rstrip () input rstrip split sw = bubble_sort(A, N) sw bubble_sort(A, N) bubble_sort A N print(' '.join(map(str, A))) print ' '.join(map(str, A)) ' '.join ' ' join map(str, A) map str A print(sw) print sw if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    N = int(input().rstrip())
    A = list(map(int, input().rstrip().split()))
     
    sw = bubble_sort(A, N)                            

    print(' '.join(map(str, A)))
    print(sw) def main():
    N = int(input().rstrip())
    A = list(map(int, input().rstrip().split()))
     
    sw = bubble_sort(A, N)                            

    print(' '.join(map(str, A)))
    print(sw) main def bubble_sort(A, N):
    sw = 0
    flag = True
    while flag:
        flag = False
        for j in reversed(range(1, N)):
            if  A[j - 1] > A[j]:
                A[j - 1],   A[j] = A[j], A[j - 1]
                sw += 1
                flag = True
    return sw def bubble_sort(A, N):
    sw = 0
    flag = True
    while flag:
        flag = False
        for j in reversed(range(1, N)):
            if  A[j - 1] > A[j]:
                A[j - 1],   A[j] = A[j], A[j - 1]
                sw += 1
                flag = True
    return sw bubble_sort
def bubble_sort(A, N):
    sw = 0
    flag = True
    while flag:
        flag = False
        for j in reversed(range(1, N)):
            if  A[j - 1] > A[j]:
                A[j - 1],   A[j] = A[j], A[j - 1]
                sw += 1
                flag = True
    return sw

def main():
    N = int(input().rstrip())
    A = list(map(int, input().rstrip().split()))
     
    sw = bubble_sort(A, N)                            

    print(' '.join(map(str, A)))
    print(sw)

if __name__ == '__main__':
    main()