// ALDS1_2_A Bubble Sort
import java.io.*;

class Main {
   public static void main(String[] args) {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      
      try {
         int N = Integer.parseInt(br.readLine());
         int[] A = new int[N];
         String[] inputAry = br.readLine().split(" ");
         for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(inputAry[i]);
         }
         bubblesort(A, N);
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   }
   static void bubblesort(int[] A, int N) {
      int cnt = 0;
      int tmp;
      boolean flag = true;
      while (flag) {
         flag = false;
         for (int j = N - 1; j >= 1; j--){
            if (A[j] < A[j - 1]) {
               tmp = A[j];
               A[j] = A[j - 1];
               A[j - 1] = tmp;
               cnt++;
               flag = true;
            }
         }
      }
      for (int i = 0; i < N; i++) {
         if (i == N - 1) {
            System.out.println(A[i]);
         } else {
            System.out.print(A[i] + " ");
         }
      }
      System.out.println(cnt);
   }
} import java.io.*; io.* java class Main {
   public static void main(String[] args) {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      
      try {
         int N = Integer.parseInt(br.readLine());
         int[] A = new int[N];
         String[] inputAry = br.readLine().split(" ");
         for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(inputAry[i]);
         }
         bubblesort(A, N);
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   }
   static void bubblesort(int[] A, int N) {
      int cnt = 0;
      int tmp;
      boolean flag = true;
      while (flag) {
         flag = false;
         for (int j = N - 1; j >= 1; j--){
            if (A[j] < A[j - 1]) {
               tmp = A[j];
               A[j] = A[j - 1];
               A[j - 1] = tmp;
               cnt++;
               flag = true;
            }
         }
      }
      for (int i = 0; i < N; i++) {
         if (i == N - 1) {
            System.out.println(A[i]);
         } else {
            System.out.print(A[i] + " ");
         }
      }
      System.out.println(cnt);
   }
} Main public static void main(String[] args) {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      
      try {
         int N = Integer.parseInt(br.readLine());
         int[] A = new int[N];
         String[] inputAry = br.readLine().split(" ");
         for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(inputAry[i]);
         }
         bubblesort(A, N);
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   } main {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      
      try {
         int N = Integer.parseInt(br.readLine());
         int[] A = new int[N];
         String[] inputAry = br.readLine().split(" ");
         for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(inputAry[i]);
         }
         bubblesort(A, N);
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) try {
         int N = Integer.parseInt(br.readLine());
         int[] A = new int[N];
         String[] inputAry = br.readLine().split(" ");
         for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(inputAry[i]);
         }
         bubblesort(A, N);
      }
      catch (Exception e) {
         e.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      } Exception e {
         e.printStackTrace();
      } e.printStackTrace() e.printStackTrace e {
         int N = Integer.parseInt(br.readLine());
         int[] A = new int[N];
         String[] inputAry = br.readLine().split(" ");
         for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(inputAry[i]);
         }
         bubblesort(A, N);
      } int N = Integer.parseInt(br.readLine()); N Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br int[] A = new int[N]; A new int[N] N String[] inputAry = br.readLine().split(" "); inputAry br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(inputAry[i]);
         } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            A[i] = Integer.parseInt(inputAry[i]);
         } {
            A[i] = Integer.parseInt(inputAry[i]);
         } A[i] = Integer.parseInt(inputAry[i]) A[i] A i Integer.parseInt(inputAry[i]) Integer.parseInt Integer inputAry[i] inputAry i bubblesort(A, N) bubblesort A N String[] args args static void bubblesort(int[] A, int N) {
      int cnt = 0;
      int tmp;
      boolean flag = true;
      while (flag) {
         flag = false;
         for (int j = N - 1; j >= 1; j--){
            if (A[j] < A[j - 1]) {
               tmp = A[j];
               A[j] = A[j - 1];
               A[j - 1] = tmp;
               cnt++;
               flag = true;
            }
         }
      }
      for (int i = 0; i < N; i++) {
         if (i == N - 1) {
            System.out.println(A[i]);
         } else {
            System.out.print(A[i] + " ");
         }
      }
      System.out.println(cnt);
   } bubblesort {
      int cnt = 0;
      int tmp;
      boolean flag = true;
      while (flag) {
         flag = false;
         for (int j = N - 1; j >= 1; j--){
            if (A[j] < A[j - 1]) {
               tmp = A[j];
               A[j] = A[j - 1];
               A[j - 1] = tmp;
               cnt++;
               flag = true;
            }
         }
      }
      for (int i = 0; i < N; i++) {
         if (i == N - 1) {
            System.out.println(A[i]);
         } else {
            System.out.print(A[i] + " ");
         }
      }
      System.out.println(cnt);
   } int cnt = 0; cnt 0 int tmp; tmp boolean flag = true; flag true while (flag) {
         flag = false;
         for (int j = N - 1; j >= 1; j--){
            if (A[j] < A[j - 1]) {
               tmp = A[j];
               A[j] = A[j - 1];
               A[j - 1] = tmp;
               cnt++;
               flag = true;
            }
         }
      } flag {
         flag = false;
         for (int j = N - 1; j >= 1; j--){
            if (A[j] < A[j - 1]) {
               tmp = A[j];
               A[j] = A[j - 1];
               A[j - 1] = tmp;
               cnt++;
               flag = true;
            }
         }
      } flag = false flag false for (int j = N - 1; j >= 1; j--){
            if (A[j] < A[j - 1]) {
               tmp = A[j];
               A[j] = A[j - 1];
               A[j - 1] = tmp;
               cnt++;
               flag = true;
            }
         } int j = N - 1; int j = N - 1; j N - 1 N 1 j >= 1 j 1 j-- j-- j {
            if (A[j] < A[j - 1]) {
               tmp = A[j];
               A[j] = A[j - 1];
               A[j - 1] = tmp;
               cnt++;
               flag = true;
            }
         } {
            if (A[j] < A[j - 1]) {
               tmp = A[j];
               A[j] = A[j - 1];
               A[j - 1] = tmp;
               cnt++;
               flag = true;
            }
         } if (A[j] < A[j - 1]) {
               tmp = A[j];
               A[j] = A[j - 1];
               A[j - 1] = tmp;
               cnt++;
               flag = true;
            } A[j] < A[j - 1] A[j] A j A[j - 1] A j - 1 j 1 {
               tmp = A[j];
               A[j] = A[j - 1];
               A[j - 1] = tmp;
               cnt++;
               flag = true;
            } 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 cnt++ cnt flag = true flag true for (int i = 0; i < N; i++) {
         if (i == N - 1) {
            System.out.println(A[i]);
         } else {
            System.out.print(A[i] + " ");
         }
      } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
         if (i == N - 1) {
            System.out.println(A[i]);
         } else {
            System.out.print(A[i] + " ");
         }
      } {
         if (i == N - 1) {
            System.out.println(A[i]);
         } else {
            System.out.print(A[i] + " ");
         }
      } if (i == N - 1) {
            System.out.println(A[i]);
         } else {
            System.out.print(A[i] + " ");
         } i == N - 1 i N - 1 N 1 {
            System.out.println(A[i]);
         } System.out.println(A[i]) System.out.println System.out System System.out A[i] A i {
            System.out.print(A[i] + " ");
         } System.out.print(A[i] + " ") System.out.print System.out System System.out A[i] + " " A[i] A i " " System.out.println(cnt) System.out.println System.out System System.out cnt int[] A A int N N
// ALDS1_2_A Bubble Sort
import java.io.*;

class Main {
   public static void main(String[] args) {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      
      try {
         int N = Integer.parseInt(br.readLine());
         int[] A = new int[N];
         String[] inputAry = br.readLine().split(" ");
         for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(inputAry[i]);
         }
         bubblesort(A, N);
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   }
   static void bubblesort(int[] A, int N) {
      int cnt = 0;
      int tmp;
      boolean flag = true;
      while (flag) {
         flag = false;
         for (int j = N - 1; j >= 1; j--){
            if (A[j] < A[j - 1]) {
               tmp = A[j];
               A[j] = A[j - 1];
               A[j - 1] = tmp;
               cnt++;
               flag = true;
            }
         }
      }
      for (int i = 0; i < N; i++) {
         if (i == N - 1) {
            System.out.println(A[i]);
         } else {
            System.out.print(A[i] + " ");
         }
      }
      System.out.println(cnt);
   }
}