import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] a = new int[n];
    for(int i = 0; i<n; i++) {
      a[i] =sc.nextInt();
    }
    int count = bubbleSort(a, n);
    for(int i = 0; i<n; i++) {
      if(i<n-1){
        System.out.print(a[i] + " ");
      }else {
        System.out.println(a[i]);
      }
    }
    System.out.println(count);
  }

  public static int bubbleSort(int[] a,int n) {
    boolean flag = true;
    int count = 0;
    while(flag == true) {
      flag = false;
      for(int j = n-1;j>0; j--) {
        if(a[j]<a[j-1]) {
          int free = a[j];
          a[j] = a[j-1];
          a[j-1] = free;
          count++;
          flag = true;
        }
      }
    }
    return count;
  }
} import java.util.Scanner; Scanner java.util public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] a = new int[n];
    for(int i = 0; i<n; i++) {
      a[i] =sc.nextInt();
    }
    int count = bubbleSort(a, n);
    for(int i = 0; i<n; i++) {
      if(i<n-1){
        System.out.print(a[i] + " ");
      }else {
        System.out.println(a[i]);
      }
    }
    System.out.println(count);
  }

  public static int bubbleSort(int[] a,int n) {
    boolean flag = true;
    int count = 0;
    while(flag == true) {
      flag = false;
      for(int j = n-1;j>0; j--) {
        if(a[j]<a[j-1]) {
          int free = a[j];
          a[j] = a[j-1];
          a[j-1] = free;
          count++;
          flag = true;
        }
      }
    }
    return count;
  }
} Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] a = new int[n];
    for(int i = 0; i<n; i++) {
      a[i] =sc.nextInt();
    }
    int count = bubbleSort(a, n);
    for(int i = 0; i<n; i++) {
      if(i<n-1){
        System.out.print(a[i] + " ");
      }else {
        System.out.println(a[i]);
      }
    }
    System.out.println(count);
  } main {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] a = new int[n];
    for(int i = 0; i<n; i++) {
      a[i] =sc.nextInt();
    }
    int count = bubbleSort(a, n);
    for(int i = 0; i<n; i++) {
      if(i<n-1){
        System.out.print(a[i] + " ");
      }else {
        System.out.println(a[i]);
      }
    }
    System.out.println(count);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] a = new int[n]; a new int[n] n for(int i = 0; i<n; i++) {
      a[i] =sc.nextInt();
    } int i = 0; int i = 0; i 0 i<n i n i++ i++ i {
      a[i] =sc.nextInt();
    } {
      a[i] =sc.nextInt();
    } a[i] =sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc int count = bubbleSort(a, n); count bubbleSort(a, n) bubbleSort a n for(int i = 0; i<n; i++) {
      if(i<n-1){
        System.out.print(a[i] + " ");
      }else {
        System.out.println(a[i]);
      }
    } int i = 0; int i = 0; i 0 i<n i n i++ i++ i {
      if(i<n-1){
        System.out.print(a[i] + " ");
      }else {
        System.out.println(a[i]);
      }
    } {
      if(i<n-1){
        System.out.print(a[i] + " ");
      }else {
        System.out.println(a[i]);
      }
    } if(i<n-1){
        System.out.print(a[i] + " ");
      }else {
        System.out.println(a[i]);
      } i<n-1 i n-1 n 1 {
        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(a[i]);
      } System.out.println(a[i]) System.out.println System.out System System.out a[i] a i System.out.println(count) System.out.println System.out System System.out count String[] args args public static int bubbleSort(int[] a,int n) {
    boolean flag = true;
    int count = 0;
    while(flag == true) {
      flag = false;
      for(int j = n-1;j>0; j--) {
        if(a[j]<a[j-1]) {
          int free = a[j];
          a[j] = a[j-1];
          a[j-1] = free;
          count++;
          flag = true;
        }
      }
    }
    return count;
  } bubbleSort {
    boolean flag = true;
    int count = 0;
    while(flag == true) {
      flag = false;
      for(int j = n-1;j>0; j--) {
        if(a[j]<a[j-1]) {
          int free = a[j];
          a[j] = a[j-1];
          a[j-1] = free;
          count++;
          flag = true;
        }
      }
    }
    return count;
  } boolean flag = true; flag true int count = 0; count 0 while(flag == true) {
      flag = false;
      for(int j = n-1;j>0; j--) {
        if(a[j]<a[j-1]) {
          int free = a[j];
          a[j] = a[j-1];
          a[j-1] = free;
          count++;
          flag = true;
        }
      }
    } flag == true flag true {
      flag = false;
      for(int j = n-1;j>0; j--) {
        if(a[j]<a[j-1]) {
          int free = a[j];
          a[j] = a[j-1];
          a[j-1] = free;
          count++;
          flag = true;
        }
      }
    } flag = false flag false for(int j = n-1;j>0; j--) {
        if(a[j]<a[j-1]) {
          int free = a[j];
          a[j] = a[j-1];
          a[j-1] = free;
          count++;
          flag = true;
        }
      } int j = n-1; int j = n-1; j n-1 n 1 j>0 j 0 j-- j-- j {
        if(a[j]<a[j-1]) {
          int free = a[j];
          a[j] = a[j-1];
          a[j-1] = free;
          count++;
          flag = true;
        }
      } {
        if(a[j]<a[j-1]) {
          int free = a[j];
          a[j] = a[j-1];
          a[j-1] = free;
          count++;
          flag = true;
        }
      } if(a[j]<a[j-1]) {
          int free = a[j];
          a[j] = a[j-1];
          a[j-1] = free;
          count++;
          flag = true;
        } a[j]<a[j-1] a[j] a j a[j-1] a j-1 j 1 {
          int free = a[j];
          a[j] = a[j-1];
          a[j-1] = free;
          count++;
          flag = true;
        } int free = a[j]; free a[j] a j a[j] = a[j-1] a[j] a j a[j-1] a j-1 j 1 a[j-1] = free a[j-1] a j-1 j 1 free count++ count flag = true flag true return count; count int[] a a int n n public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] a = new int[n];
    for(int i = 0; i<n; i++) {
      a[i] =sc.nextInt();
    }
    int count = bubbleSort(a, n);
    for(int i = 0; i<n; i++) {
      if(i<n-1){
        System.out.print(a[i] + " ");
      }else {
        System.out.println(a[i]);
      }
    }
    System.out.println(count);
  }

  public static int bubbleSort(int[] a,int n) {
    boolean flag = true;
    int count = 0;
    while(flag == true) {
      flag = false;
      for(int j = n-1;j>0; j--) {
        if(a[j]<a[j-1]) {
          int free = a[j];
          a[j] = a[j-1];
          a[j-1] = free;
          count++;
          flag = true;
        }
      }
    }
    return count;
  }
} public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] a = new int[n];
    for(int i = 0; i<n; i++) {
      a[i] =sc.nextInt();
    }
    int count = bubbleSort(a, n);
    for(int i = 0; i<n; i++) {
      if(i<n-1){
        System.out.print(a[i] + " ");
      }else {
        System.out.println(a[i]);
      }
    }
    System.out.println(count);
  }

  public static int bubbleSort(int[] a,int n) {
    boolean flag = true;
    int count = 0;
    while(flag == true) {
      flag = false;
      for(int j = n-1;j>0; j--) {
        if(a[j]<a[j-1]) {
          int free = a[j];
          a[j] = a[j-1];
          a[j-1] = free;
          count++;
          flag = true;
        }
      }
    }
    return count;
  }
} Main
import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] a = new int[n];
    for(int i = 0; i<n; i++) {
      a[i] =sc.nextInt();
    }
    int count = bubbleSort(a, n);
    for(int i = 0; i<n; i++) {
      if(i<n-1){
        System.out.print(a[i] + " ");
      }else {
        System.out.println(a[i]);
      }
    }
    System.out.println(count);
  }

  public static int bubbleSort(int[] a,int n) {
    boolean flag = true;
    int count = 0;
    while(flag == true) {
      flag = false;
      for(int j = n-1;j>0; j--) {
        if(a[j]<a[j-1]) {
          int free = a[j];
          a[j] = a[j-1];
          a[j-1] = free;
          count++;
          flag = true;
        }
      }
    }
    return count;
  }
}