AtCoder Beginner Contest 302 D

https://atcoder.jp/contests/abc302/tasks/abc302_d

Aと同じか小さい一番大きなBを尺取り法で探します。逆もやります。

// Impartial Gift
#![allow(non_snake_case)]

use std::cmp::max;


//////////////////// library ////////////////////

fn read<T: std::str::FromStr>() -> T {
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).ok();
    line.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace()
            .map(|e| e.parse().ok().unwrap()).collect()
}

//////////////////// process ////////////////////

fn read_input() -> (i64, Vec<i64>, Vec<i64>) {
    let v = read_vec();
    let D = v[2];
    let A = read_vec();
    let B = read_vec();
    (D, A, B)
}

fn f1(A: &Vec<i64>, B: &Vec<i64>, D: i64) -> i64 {
    let mut k = A.len() - 1;
    let mut l = B.len() - 1;
    while k < A.len() && l < B.len() {
        if A[k] >= B[l] {
            if A[k] - B[l] <= D {
                return A[k] + B[l]
            }
            else {
                k -= 1
            }
        }
        else {
            l -= 1
        }
    }
    -1
}

fn f(D: i64, mut A: Vec<i64>, mut B: Vec<i64>) -> i64 {
    A.sort();
    B.sort();
    max(f1(&A, &B, D), f1(&B, &A, D))
}

//////////////////// main ////////////////////

fn main() {
    let (D, A, B) = read_input();
    println!("{}", f(D, A, B))
}