import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String AStr[] = br.readLine().split(" ");
		int A[] = new int[n];

		for(int i = 0 ; i < n ; i++) A[i] = Integer.parseInt(AStr[i]);

		bubbleSort(n,A);

	}

	public static void bubbleSort(int n, int[] A){
		boolean flag = true;
		int count = 0;
		int y = 1;
		while(flag){
			flag = false;
			for(int i = n - 1 ; y <= i ; i-- ){
				if(A[i] < A[i - 1]){
					int tmp = A[i];
					A[i] = A[i - 1];
					A[i - 1] = tmp;
					flag = true;
					count++;
				}
			}
			y++;
		}

		StringBuilder sb = new StringBuilder();
		for(int i = 0 ; i < n; i++){
			sb.append(A[i]);
			sb.append(" ");
		}
		sb.deleteCharAt(sb.length() - 1);
		System.out.println(sb.toString());
		System.out.println(count);

	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String AStr[] = br.readLine().split(" ");
		int A[] = new int[n];

		for(int i = 0 ; i < n ; i++) A[i] = Integer.parseInt(AStr[i]);

		bubbleSort(n,A);

	}

	public static void bubbleSort(int n, int[] A){
		boolean flag = true;
		int count = 0;
		int y = 1;
		while(flag){
			flag = false;
			for(int i = n - 1 ; y <= i ; i-- ){
				if(A[i] < A[i - 1]){
					int tmp = A[i];
					A[i] = A[i - 1];
					A[i - 1] = tmp;
					flag = true;
					count++;
				}
			}
			y++;
		}

		StringBuilder sb = new StringBuilder();
		for(int i = 0 ; i < n; i++){
			sb.append(A[i]);
			sb.append(" ");
		}
		sb.deleteCharAt(sb.length() - 1);
		System.out.println(sb.toString());
		System.out.println(count);

	}
} Main public static void main(String[] args) throws NumberFormatException, IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String AStr[] = br.readLine().split(" ");
		int A[] = new int[n];

		for(int i = 0 ; i < n ; i++) A[i] = Integer.parseInt(AStr[i]);

		bubbleSort(n,A);

	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String AStr[] = br.readLine().split(" ");
		int A[] = new int[n];

		for(int i = 0 ; i < n ; i++) A[i] = Integer.parseInt(AStr[i]);

		bubbleSort(n,A);

	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int n = Integer.parseInt(br.readLine()); n Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br String AStr[] = br.readLine().split(" "); AStr br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int A[] = new int[n]; A new int[n] n for(int i = 0 ; i < n ; i++) A[i] = Integer.parseInt(AStr[i]); int i = 0 ; int i = 0 ; i 0 i < n i n i++ i++ i A[i] = Integer.parseInt(AStr[i]); A[i] = Integer.parseInt(AStr[i]) A[i] A i Integer.parseInt(AStr[i]) Integer.parseInt Integer AStr[i] AStr i bubbleSort(n,A) bubbleSort n A String[] args args public static void bubbleSort(int n, int[] A){
		boolean flag = true;
		int count = 0;
		int y = 1;
		while(flag){
			flag = false;
			for(int i = n - 1 ; y <= i ; i-- ){
				if(A[i] < A[i - 1]){
					int tmp = A[i];
					A[i] = A[i - 1];
					A[i - 1] = tmp;
					flag = true;
					count++;
				}
			}
			y++;
		}

		StringBuilder sb = new StringBuilder();
		for(int i = 0 ; i < n; i++){
			sb.append(A[i]);
			sb.append(" ");
		}
		sb.deleteCharAt(sb.length() - 1);
		System.out.println(sb.toString());
		System.out.println(count);

	} bubbleSort {
		boolean flag = true;
		int count = 0;
		int y = 1;
		while(flag){
			flag = false;
			for(int i = n - 1 ; y <= i ; i-- ){
				if(A[i] < A[i - 1]){
					int tmp = A[i];
					A[i] = A[i - 1];
					A[i - 1] = tmp;
					flag = true;
					count++;
				}
			}
			y++;
		}

		StringBuilder sb = new StringBuilder();
		for(int i = 0 ; i < n; i++){
			sb.append(A[i]);
			sb.append(" ");
		}
		sb.deleteCharAt(sb.length() - 1);
		System.out.println(sb.toString());
		System.out.println(count);

	} boolean flag = true; flag true int count = 0; count 0 int y = 1; y 1 while(flag){
			flag = false;
			for(int i = n - 1 ; y <= i ; i-- ){
				if(A[i] < A[i - 1]){
					int tmp = A[i];
					A[i] = A[i - 1];
					A[i - 1] = tmp;
					flag = true;
					count++;
				}
			}
			y++;
		} flag {
			flag = false;
			for(int i = n - 1 ; y <= i ; i-- ){
				if(A[i] < A[i - 1]){
					int tmp = A[i];
					A[i] = A[i - 1];
					A[i - 1] = tmp;
					flag = true;
					count++;
				}
			}
			y++;
		} flag = false flag false for(int i = n - 1 ; y <= i ; i-- ){
				if(A[i] < A[i - 1]){
					int tmp = A[i];
					A[i] = A[i - 1];
					A[i - 1] = tmp;
					flag = true;
					count++;
				}
			} int i = n - 1 ; int i = n - 1 ; i n - 1 n 1 y <= i y i i-- i-- i {
				if(A[i] < A[i - 1]){
					int tmp = A[i];
					A[i] = A[i - 1];
					A[i - 1] = tmp;
					flag = true;
					count++;
				}
			} {
				if(A[i] < A[i - 1]){
					int tmp = A[i];
					A[i] = A[i - 1];
					A[i - 1] = tmp;
					flag = true;
					count++;
				}
			} if(A[i] < A[i - 1]){
					int tmp = A[i];
					A[i] = A[i - 1];
					A[i - 1] = tmp;
					flag = true;
					count++;
				} A[i] < A[i - 1] A[i] A i A[i - 1] A i - 1 i 1 {
					int tmp = A[i];
					A[i] = A[i - 1];
					A[i - 1] = tmp;
					flag = true;
					count++;
				} int tmp = A[i]; tmp A[i] A i A[i] = A[i - 1] A[i] A i A[i - 1] A i - 1 i 1 A[i - 1] = tmp A[i - 1] A i - 1 i 1 tmp flag = true flag true count++ count y++ y StringBuilder sb = new StringBuilder(); sb new StringBuilder() for(int i = 0 ; i < n; i++){
			sb.append(A[i]);
			sb.append(" ");
		} int i = 0 ; int i = 0 ; i 0 i < n i n i++ i++ i {
			sb.append(A[i]);
			sb.append(" ");
		} {
			sb.append(A[i]);
			sb.append(" ");
		} sb.append(A[i]) sb.append sb A[i] A i sb.append(" ") sb.append sb " " sb.deleteCharAt(sb.length() - 1) sb.deleteCharAt sb sb.length() - 1 sb.length() sb.length sb 1 System.out.println(sb.toString()) System.out.println System.out System System.out sb.toString() sb.toString sb System.out.println(count) System.out.println System.out System System.out count int n n int[] A A public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String AStr[] = br.readLine().split(" ");
		int A[] = new int[n];

		for(int i = 0 ; i < n ; i++) A[i] = Integer.parseInt(AStr[i]);

		bubbleSort(n,A);

	}

	public static void bubbleSort(int n, int[] A){
		boolean flag = true;
		int count = 0;
		int y = 1;
		while(flag){
			flag = false;
			for(int i = n - 1 ; y <= i ; i-- ){
				if(A[i] < A[i - 1]){
					int tmp = A[i];
					A[i] = A[i - 1];
					A[i - 1] = tmp;
					flag = true;
					count++;
				}
			}
			y++;
		}

		StringBuilder sb = new StringBuilder();
		for(int i = 0 ; i < n; i++){
			sb.append(A[i]);
			sb.append(" ");
		}
		sb.deleteCharAt(sb.length() - 1);
		System.out.println(sb.toString());
		System.out.println(count);

	}
} public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String AStr[] = br.readLine().split(" ");
		int A[] = new int[n];

		for(int i = 0 ; i < n ; i++) A[i] = Integer.parseInt(AStr[i]);

		bubbleSort(n,A);

	}

	public static void bubbleSort(int n, int[] A){
		boolean flag = true;
		int count = 0;
		int y = 1;
		while(flag){
			flag = false;
			for(int i = n - 1 ; y <= i ; i-- ){
				if(A[i] < A[i - 1]){
					int tmp = A[i];
					A[i] = A[i - 1];
					A[i - 1] = tmp;
					flag = true;
					count++;
				}
			}
			y++;
		}

		StringBuilder sb = new StringBuilder();
		for(int i = 0 ; i < n; i++){
			sb.append(A[i]);
			sb.append(" ");
		}
		sb.deleteCharAt(sb.length() - 1);
		System.out.println(sb.toString());
		System.out.println(count);

	}
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String AStr[] = br.readLine().split(" ");
		int A[] = new int[n];

		for(int i = 0 ; i < n ; i++) A[i] = Integer.parseInt(AStr[i]);

		bubbleSort(n,A);

	}

	public static void bubbleSort(int n, int[] A){
		boolean flag = true;
		int count = 0;
		int y = 1;
		while(flag){
			flag = false;
			for(int i = n - 1 ; y <= i ; i-- ){
				if(A[i] < A[i - 1]){
					int tmp = A[i];
					A[i] = A[i - 1];
					A[i - 1] = tmp;
					flag = true;
					count++;
				}
			}
			y++;
		}

		StringBuilder sb = new StringBuilder();
		for(int i = 0 ; i < n; i++){
			sb.append(A[i]);
			sb.append(" ");
		}
		sb.deleteCharAt(sb.length() - 1);
		System.out.println(sb.toString());
		System.out.println(count);

	}
}