Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python
# encoding: utf-8


class Solution:
    """
    @param prices: Given an integer array
    @return: Maximum profit
    """

    @staticmethod
    def swap(a, b):
        a, b = b, a
        return a, b

    @staticmethod
    def bubble_sort(self):
        # write your code here
        array_length = int(input())
        unsorted_array = [int(x) for x in input().split()]

        flag = 1
        count = 0
        while flag:
            flag = 0
            for j in range(array_length - 1, 0, -1):
                if unsorted_array[j] < unsorted_array[j - 1]:
                    self.swap(unsorted_array[j], unsorted_array[j - 1])
                    flag = 1
                    count += 1
        print(" ".join(map(str, unsorted_array)))
        print(str(count))


if __name__ == '__main__':
    solution = Solution()
    solution.bubble_sort()

Fixed submission:
#!/usr/bin/env python
# encoding: utf-8


class Solution:

    @staticmethod
    def bubble_sort():
        # write your code here
        array_length = int(input())
        array = [int(x) for x in input().split()]

        flag, count, cursor = 1, 0, 0
        while flag:
            flag = 0
            for j in range(array_length - 1, cursor, -1):
                if array[j] < array[j - 1]:
                    array[j], array[j - 1] = array[j - 1], array[j]
                    flag = 1
                    count += 1
        cursor += 1
        print(" ".join(map(str, array)))
        print(str(count))


if __name__ == '__main__':
    solution = Solution()
    solution.bubble_sort()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
# encoding: utf-8


class Solution:
    """
    @param prices: Given an integer array
    @return: Maximum profit
    """

    @staticmethod
    def swap(a, b):
        a, b = b, a
        return a, b

    def bubble_sort(self):
        # write your code here
        array_length = int(input())
        unsorted_array = [int(x) for x in input().split()]

        flag = 1
        count = 0
        while flag:
            flag = 0
            for j in range(array_length - 1, 0, -1):
                if unsorted_array[j] < unsorted_array[j - 1]:
                    self.swap(unsorted_array[j], unsorted_array[j - 1])
                    flag = 1
                    count += 1
        print(" ".join(map(str, unsorted_array)))
        print(str(count))


if __name__ == '__main__':
    solution = Solution()
    solution.bubble_sort()

Fixed submission:
#!/usr/bin/env python
# encoding: utf-8


class Solution:

    @staticmethod
    def bubble_sort():
        # write your code here
        array_length = int(input())
        array = [int(x) for x in input().split()]

        flag, count, cursor = 1, 0, 0
        while flag:
            flag = 0
            for j in range(array_length - 1, cursor, -1):
                if array[j] < array[j - 1]:
                    array[j], array[j - 1] = array[j - 1], array[j]
                    flag = 1
                    count += 1
        cursor += 1
        print(" ".join(map(str, array)))
        print(str(count))


if __name__ == '__main__':
    solution = Solution()
    solution.bubble_sort()