def bubbleSort(a, n):
    count = 0
    flag = 1
    while flag:
        flag = 0
        for j in range(n - 1, 0, -1):
            if a[j] < a[j - 1]:
                a[j], a[j - 1] = a[j - 1], a[j]
                count += 1
                flag = 1
    return count


def main():
    n = int(input())
    a = [int(x) for x in input().split(' ')]
    count = bubbleSort(a, n)
    print(' '.join([str(x) for x in a]))
    print(count)

if __name__ == '__main__':
    main() def bubbleSort(a, n):
    count = 0
    flag = 1
    while flag:
        flag = 0
        for j in range(n - 1, 0, -1):
            if a[j] < a[j - 1]:
                a[j], a[j - 1] = a[j - 1], a[j]
                count += 1
                flag = 1
    return count bubbleSort count = 0 count 0 flag = 1 flag 1 while flag:
        flag = 0
        for j in range(n - 1, 0, -1):
            if a[j] < a[j - 1]:
                a[j], a[j - 1] = a[j - 1], a[j]
                count += 1
                flag = 1
     flag flag = 0 flag 0 for j in range(n - 1, 0, -1):
            if a[j] < a[j - 1]:
                a[j], a[j - 1] = a[j - 1], a[j]
                count += 1
                flag = 1
     j range(n - 1, 0, -1) range n - 1 n 1 0 -1 if a[j] < a[j - 1]:
                a[j], a[j - 1] = a[j - 1], a[j]
                count += 1
                flag = 1
     a[j] < a[j - 1] a[j] a j a[j - 1] a j - 1 j 1 a[j], a[j - 1] = a[j - 1], a[j] a[j] a j a[j - 1] a j - 1 j 1 a[j - 1] a j - 1 j 1 a[j] a j count += 1 count 1 flag = 1 flag 1 return count count a a n n def main():
    n = int(input())
    a = [int(x) for x in input().split(' ')]
    count = bubbleSort(a, n)
    print(' '.join([str(x) for x in a]))
    print(count) main n = int(input()) n int(input()) int input() input int(x) for x in input().split(' ') for x in input().split(' ') x input().split(' ') ().split () input split ' ' for x in input().split(' ') int(x) int x a = [int(x) for x in input().split(' ')] a [int(x) for x in input().split(' ')] count = bubbleSort(a, n) count bubbleSort(a, n) bubbleSort a n str(x) for x in a for x in a x a for x in a str(x) str x print(' '.join([str(x) for x in a])) print ' '.join([str(x) for x in a]) ' '.join ' ' join [str(x) for x in a] print(count) print count if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def bubbleSort(a, n):
    count = 0
    flag = 1
    while flag:
        flag = 0
        for j in range(n - 1, 0, -1):
            if a[j] < a[j - 1]:
                a[j], a[j - 1] = a[j - 1], a[j]
                count += 1
                flag = 1
    return count def bubbleSort(a, n):
    count = 0
    flag = 1
    while flag:
        flag = 0
        for j in range(n - 1, 0, -1):
            if a[j] < a[j - 1]:
                a[j], a[j - 1] = a[j - 1], a[j]
                count += 1
                flag = 1
    return count bubbleSort def main():
    n = int(input())
    a = [int(x) for x in input().split(' ')]
    count = bubbleSort(a, n)
    print(' '.join([str(x) for x in a]))
    print(count) def main():
    n = int(input())
    a = [int(x) for x in input().split(' ')]
    count = bubbleSort(a, n)
    print(' '.join([str(x) for x in a]))
    print(count) main
def bubbleSort(a, n):
    count = 0
    flag = 1
    while flag:
        flag = 0
        for j in range(n - 1, 0, -1):
            if a[j] < a[j - 1]:
                a[j], a[j - 1] = a[j - 1], a[j]
                count += 1
                flag = 1
    return count


def main():
    n = int(input())
    a = [int(x) for x in input().split(' ')]
    count = bubbleSort(a, n)
    print(' '.join([str(x) for x in a]))
    print(count)

if __name__ == '__main__':
    main()