#![allow(unused_imports)] fn main() { input! { n: usize, m: usize, k: usize, a: [[i64; m]; n], } let (mut ok, mut ng) = (0i64, 1_000_000_001); while ok.abs_diff(ng) > 1 { let mid = (ok + ng) / 2; let mut b = mvec![0; (n, m)]; for i in 0..n { for j in 0..m { if a[i][j] < mid { b[i][j] = 1; } } } let mut q = VecDeque::<(usize, usize)>::new(); let mut cost = mvec![1<<30; (n, m)]; cost[0][0] = b[0][0]; q.push_back((0, 0)); while let Some((i, j)) = q.pop_front() { for (di, dj) in [(1, 0), (0, 1), (-1, 0), (0, -1)] { let ni = i.wrapping_add_signed(di); let nj = j.wrapping_add_signed(dj); if ni < n && nj < m && chmin!(cost[ni][nj], cost[i][j] + b[ni][nj]) { if b[ni][nj] == 0 { q.push_front((ni, nj)); } else { q.push_back((ni, nj)); } } } } *if cost[n - 1][m - 1] <= k { &mut ok } else { &mut ng } = mid; } println!("{ok}"); } use proconio::{input, marker::*}; use std::{cmp::Reverse, collections::*}; #[macro_export] macro_rules! chmax { ($a:expr, $b:expr) => {{ let tmp = $b; if $a < tmp { $a = tmp; true } else { false } }}; } #[macro_export] macro_rules! chmin { ($a:expr, $b:expr) => {{ let tmp = $b; if $a > tmp { $a = tmp; true } else { false } }}; } #[macro_export] /// mvec![] macro_rules! mvec { ($val:expr; ()) => { $val }; ($val:expr; ($size:expr $(,$rest:expr)*)) => { vec![mvec![$val; ($($rest),*)]; $size] }; }