import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.BitSet; import java.util.HashMap; import java.util.TreeSet; public class Main{ public static void main(String[] args){ try {(new Solve()).solve();} catch (NumberFormatException | IOException e) {e.printStackTrace();} } } class Solve{ void solve() throws NumberFormatException, IOException{ final ContestScanner in = new ContestScanner(); ContestWriter out = new ContestWriter(); int n = in.nextInt(); int[] a = new int[n]; int[] b = new int[n]; HashMap first = new HashMap<>(); for(int i=0; i second = new HashMap<>(); for(int i=n-1; i>=0; i--){ if(second.containsKey(a[i])){ if(first.get(a[i])!=i) a[i] = 0; }else if(first.get(a[i])==i){ a[i] = -a[i]; }else{ second.put(a[i], i); } } TreeSet set = new TreeSet<>((e1,e2)->e2-e1); for(int i=0; i0){ if(set.contains(a[i])){ final int t = set.first(); set.remove(a[i]); a[i] = t; continue; } else set.add(a[i]); }else if(a[i]<0){ a[i] = Math.max(-a[i], set.isEmpty()?0:set.first()); continue; } if(!set.isEmpty()) a[i] = set.first(); } StringBuilder sb = new StringBuilder(); for(int i=0; i extends HashMap{ @Override public Integer get(Object key){return containsKey(key)?super.get(key):0;} public void add(T key,int v){put(key,get(key)+v);} public void add(T key){put(key,get(key)+1);} } class ContestWriter{ private PrintWriter out; public ContestWriter(String filename) throws IOException {out = new PrintWriter(new BufferedWriter(new FileWriter(filename)));} public ContestWriter() throws IOException{out = new PrintWriter(System.out);} public void println(String str){out.println(str);} public void println(Object str){out.println(str);} public void print(String str){out.print(str);} public void close(){out.close();} } class ContestScanner { private BufferedReader reader; private String[] line; private int idx; public ContestScanner()throws FileNotFoundException {reader=new BufferedReader(new InputStreamReader(System.in));} public ContestScanner(String filename)throws FileNotFoundException {reader=new BufferedReader(new InputStreamReader(new FileInputStream(filename)));} public String nextToken()throws IOException{if(line==null||line.length<=idx){ line=reader.readLine().trim().split(" ");idx=0;}return line[idx++];} public String readLine()throws IOException{return reader.readLine();} public long nextLong()throws IOException,NumberFormatException{return Long.parseLong(nextToken());} public int nextInt()throws NumberFormatException,IOException{return(int)nextLong();} public double nextDouble()throws NumberFormatException,IOException {return Double.parseDouble(nextToken());} }