#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{BinaryHeap, HashMap};

#[allow(dead_code)]
type Graph = Vec<Vec<usize>>;

struct RollingHash {
    modp: Vec<usize>,
    base: Vec<usize>,
    hash: Vec<Vec<usize>>,
    pow: Vec<Vec<usize>>,
}

impl RollingHash {
    fn new(s: &Vec<char>) -> RollingHash {
        let n = s.len();
        let modp = vec![1000000007, 1000000009];
        let base = vec![1009, 1007];
        let mut hash = vec![vec![0; 2]; n + 1];
        let mut pow = vec![vec![1; 2]; n + 1];

        for i in 0..n {
            for j in 0..2 {
                hash[i + 1][j] = ((hash[i][j] + s[i] as usize) * base[j]) % modp[j];
                pow[i + 1][j] = (pow[i][j] * base[j]) % modp[j];
            }
        }

        RollingHash {
            modp: modp,
            base: base,
            hash: hash,
            pow: pow,
        }
    }

    // [l..r)
    fn get(&self, l: usize, r: usize) -> (usize, usize) {
        let mut res = vec![0; 2];
        for i in 0..2 {
            res[i] = (self.hash[r][i] + self.modp[i]
                - (self.hash[l][i] * self.pow[r - l][i]) % self.modp[i])
                % self.modp[i];
        }
        (res[0], res[1])
    }
}

fn binary_search<T: Ord, F>(v: &[T], f: F) -> usize
where
    F: Fn(&T) -> bool,
{
    let mut ok = v.len() as i64;
    let mut ng = -1;
    while (ok - ng).abs() > 1 {
        let mid = (ok + ng) / 2;
        if f(&v[mid as usize]) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    ok as usize
}

fn main() {
    input! {
        n: usize,
        s: chars,
    }

    let rh = RollingHash::new(&s);

    let mut ok = -1i64;
    let mut ng = n as i64;

    while (ok - ng).abs() > 1 {
        let mid = ((ok + ng) / 2) as usize;
        let mut v = vec![];

        for l in 0..n - mid + 1 {
            v.push((rh.get(l, l + mid), l));
        }
        v.sort();
        let mut found = false;
        for l in 0..n - mid + 1 {
            let hashset = rh.get(l, l + mid);
            let f = |v: &((usize, usize), usize)| -> bool { *v >= (hashset, l + mid) };
            let index = binary_search(&v, f);
            if index < v.len() && hashset == v[index].0 {
                found = true;
                break;
            }
        }
        if found {
            ok = mid as i64;
        } else {
            ng = mid as i64;
        }
    }

    println!("{}", ok);
}


Measured memory_bytes: 4352