import java.util.LinkedList; import java.util.Scanner; public class Main { private static Scanner cin; public static void main(String args[]) throws Exception { cin = new Scanner(System.in); int n = cin.nextInt(); int ret = 0; LinkedList<Integer> li…