use proconio::input; fn main() { input! { n: usize, xy: [[usize; 2]; n], } let init_score = if xy[0][0] == xy[0][1] { xy[0][0] } else { 0 }; let mut dp = [init_score; 2]; for window in xy.windows(2) { let mut next_dp = [0_usize; 2]; for from in 0..2 { for to in 0..2 { let add_score_1 = if window[0][1 - from] == window[1][to] { window[1][to] } else { 0 }; let add_score_2 = if window[1][0] == window[1][1] { window[1][0] } else { 0 }; next_dp[to] = next_dp[to].max(dp[from] + add_score_1 + add_score_2); } } dp = next_dp; } let max_score = dp[0].max(dp[1]); println!("{max_score}"); }