AtCoder Beginner Contest 257 C

https://atcoder.jp/contests/abc257/tasks/abc257_c

各体重に対して子どもが何人、大人が何人いるかをMapに入れますが、ソートして使いたいのでBTreeMapを使います。

// Robot Takahashi
#![allow(non_snake_case)]

use std::collections::BTreeMap;

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()
}

fn read_input() -> Vec<(usize, usize)> {
    let _N: usize = read();
    let S: String = read();
    let W: Vec<i32> = read_vec();
    let mut counter = BTreeMap::<i32, (usize, usize)>::new();
    for (&w, c) in W.iter().zip(S.chars()) {
        let c1 = counter.entry(w).or_insert((0, 0));
        if c == '0' {
            c1.0 += 1
        }
        else {
            c1.1 += 1
        }
    }
    counter.iter().map(|(&_w, &(n1, n2))| (n1, n2)).collect()
}

fn main() {
    let v = read_input();
    let mut num_rights: usize = v.iter().map(|&(_n1, n2)| n2).sum();
    let mut max_num_rights = num_rights;
    for (n1, n2) in v.iter() {
        num_rights += n1;
        num_rights -= n2;
        if num_rights > max_num_rights {
            max_num_rights = num_rights
        }
    }
    println!("{:?}", max_num_rights)
}