extern crate core;

use std::fmt;
use std::i32::{MAX};
use std::cmp::{Ordering, min, max };
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut, SubAssign, Range};
use std::collections::{BTreeMap, VecDeque, BinaryHeap, BTreeSet, HashMap, HashSet};
use std::fmt::{Display, Formatter, Error};

fn show<T: Display>(vec: &Vec<T>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        print!("[{}", vec[0]);
        for i in 1 .. vec.len() {
            print!(", {}", vec[i]);
        }
        println!("]");
    }
}
fn show2<T: Display>(vec: &Vec<Vec<T>>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        for l in vec {
            show(l);
        }
    }
}
macro_rules! read_lines{
    ($count: expr; $delimiter: expr; $ty: ty) => {{
        let line_count = $count;
        let mut vec: Vec<Vec<$ty>> = Vec::with_capacity(line_count);
        for _ in 0 .. line_count {
            vec.push(read_line!($delimiter; $ty));
        }
        vec
    }};
    ($count: expr; $ty: ty) => {{
        let line_count = $count;
        let mut vec: Vec<$ty> = Vec::with_capacity(line_count);
        for _ in 0 .. line_count {
            vec.push(read_value!());
        }
        vec
    }}
}
macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}
macro_rules! read_value{
    () => {
        read_line!().trim().parse().ok().unwrap()
    }
}
macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}

macro_rules! let_mut_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let mut $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
struct History {
    data_id: usize,
    access_time: usize
}
impl Eq for History{}
impl PartialEq for History {
    fn eq(&self, other: &Self) -> bool {
        other.access_time == self.access_time
    }
}
impl PartialOrd for History {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        other.access_time.partial_cmp(&self.access_time)
    }
}
impl Ord for History {
    fn cmp(&self, other: &Self) -> Ordering {
        other.access_time.cmp(&self.access_time)
    }
}
fn main(){
    let_all!(data_size: usize, cache_capacity: usize);
    let mut last_access: HashMap<usize, usize> = HashMap::new();
    let mut cache = BinaryHeap::new();
    let mut in_cache = HashMap::new();
    for time in 0usize .. data_size {
        let_all!(data: usize);
        last_access.insert(data, time);
        match in_cache.get(&data) {
            Some(&false) | None => {
                if cache.len() >= cache_capacity {
                    while let Some(History{ data_id: cache_data, access_time}) = cache.pop() {
                        match last_access.get(&cache_data) {
                            Some(&last_access_time) if access_time != last_access_time =>  cache.push(History{data_id: cache_data, access_time: last_access_time}),
                            _ => {
                                in_cache.insert(cache_data, false);
                                break
                            }
                        }
                    }
                }
                in_cache.insert(data, true);
                cache.push(History{data_id: data, access_time: time});
            }
            _ => {}
        }
    }
    let mut val: Vec<_> = in_cache.iter().filter_map(|(&id, &is_cached)| if is_cached {Some(id)} else {None}).collect();
    val.sort_by(|a, b| last_access.get(a).unwrap().cmp(last_access.get(b).unwrap()).reverse());
    for id in val {
        println!("{}", id)
    }
}


Measured memory_bytes: 15652