#![allow(unused_mut, non_snake_case,unused_imports)] use std::iter; use std::cmp::{max, min, Ordering}; use std::iter::FromIterator; use std::sync::{Arc,Mutex}; // 高速 EOF要 macro_rules! input {(source = $s:expr, $($r:tt)*) => {let mut iter = $s.split_whitespace();input_inner!{iter, $($r)*}};($($r:tt)*) => {let mut s = {use std::io::Read;let mut s = String::new();std::io::stdin().read_to_string(&mut s).unwrap();s};let mut iter = s.split_whitespace();input_inner!{iter, $($r)*}};} macro_rules! input_inner {($iter:expr) => {};($iter:expr, ) => {};($iter:expr, $var:ident : $t:tt $($r:tt)*) => {let $var = read_value!($iter, $t);input_inner!{$iter $($r)*}};} macro_rules! read_value {($iter:expr, ( $($t:tt),* )) => {( $(read_value!($iter, $t)),* )};($iter:expr, [ $t:tt ; $len:expr ]) => {(0..$len).map(|_| read_value!($iter, $t)).collect::>()};($iter:expr, chars) => {read_value!($iter, String).chars().collect::>()};($iter:expr, usize1) => {read_value!($iter, usize) - 1};($iter:expr, $t:ty) => {$iter.next().unwrap().parse::<$t>().expect("Parse error")};} // 低速 EOF不要 //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)*}};} //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)*}};} //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::>()};($next:expr, chars) => {read_value!($next, String).chars().collect::>()};($next:expr, usize1) => {read_value!($next, usize) - 1};($next:expr, $t:ty) => {$next().parse::<$t>().expect("Parse error")};} /* 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ fn calc(N:usize,V:Vec) -> usize{ let mut ret = 0; let mut T = 0; // for i in 0..N{ // for j in i+1..N{ // T = 0; // for k in 0..i{ // if V[i] > V[j]{ // 凸 // if V[i] > V[k]{ // T = max(T,V[i]); // break; // } // continue; // } // if V[i] < V[j]{ // 凹 // if V[k] > V[i]{ // T = max(T,max(V[j],V[k])); // } // } // } // for k in i+1..j{ // if V[k] > V[i] && V[k] > V[j]{// 凸 // T = max(T,V[k]); // continue; // } // if V[k] < V[i] && V[k] < V[j]{// 凹 // T = max(T,max(V[i],V[j])); // break; // } // // } // for k in j+1..N{ // if V[j] > V[i] { // 凸 // if V[j] > V[k]{ // T = max(T,V[j]); // break; // } // } // if V[j] < V[i]{ // if V[j] < V[k]{ // T = max(T,max(V[i],V[k])); // } // } // } // ret += T; // } // } let mut Lmin= 1000; let mut Lmax= 0; let Vi = V.clone(); let mut Rmin = vec![0;N]; let mut Rmax = vec![0;N]; Rmin[N-1] = 1000; Rmax[N-1] = 0; for i in (0..N-1).rev(){ Rmin[i] = min(Rmin[i+1],V[i+1]); Rmax[i] = max(Rmax[i+1],V[i+1]); } for (i,&Vi) in Vi.iter().enumerate(){ for j in i+1..N{ let Vj = V[j]; T = 0; if i != 0{ // Lmin,Lmax if Vi > Vj{ if Vi > Lmin{ T = max(T,Vi); } }else{ if Vi < Lmax{ T = max(T,max(Lmax,Vj)); } } } for k in i+1..j{ if V[k] > Vi && V[k] > Vj{// 凸 T = max(T,V[k]); continue; } if V[k] < Vi && V[k] < Vj{// 凹 T = max(T,max(Vi,Vj)); break; } } if j != N-1{ // Rmin,Rmax if Vj > Vi{ if Vj > Rmin[j]{ T = max(T,Vj); } }else{ if Vj < Rmax[j]{ T = max(T,max(Rmax[j],Vi)); } } } ret += T; } Lmax = max(Lmax,V[i]); Lmin = min(Lmin,V[i]); } return ret; } fn main() { input!(N:usize,M:usize,E:[[usize;N];M]); let N: usize = N; let M: usize = M; let mut E: Vec> = E; for i in 0..M { E[i].push(i); } let Ans = Arc::new(Mutex::new(vec![0; M])); { for V in E.into_iter() { let Ans = Ans.clone(); std::thread::spawn(move || { let &idx = V.last().unwrap(); let ret = calc(N, V); loop { let mut Ans = Ans.lock().unwrap(); if Ans[idx] == 0 { Ans[idx] = ret; break; } } }); } } std::thread::sleep_ms(1995); let mut Ans = Ans.lock().unwrap(); let &maxv = (*Ans).iter().max().unwrap(); for i in 0..M{ if maxv == Ans[i]{ println!("{}",i); break; } } // loop{ // let mut Ans = Ans.lock().unwrap(); // let mut ok = true; //// for i in 0..M{ //// if Ans[i] >= 0{continue;} //// else {ok = false;} //// } // if ok{ // // println!("{:?}",*Ans); // let &maxv = (*Ans).iter().max().unwrap(); // for i in 0..M{ // if maxv == Ans[i]{ // println!("{}",i); // break; // } // } // break; // } // } }