#![warn(clippy::pedantic)]

use std::io::{stdin, Read};

fn hash(s: &str) -> u8 {
    s.as_bytes()
        .iter()
        .copied()
        .fold(0, |acc, b| acc.wrapping_add(b).wrapping_mul(17))
}

fn main() {
    let mut input = String::new();
    stdin().read_to_string(&mut input).unwrap();
    let input = input.trim();

    let steps = input.split(',');

    println!(
        "{}",
        steps.clone().map(|s| { u64::from(hash(s)) }).sum::<u64>()
    );

    let mut boxes: [Vec<(&str, usize)>; 256] = std::array::from_fn(|_| Vec::new());

    for step in steps {
        let (label, focal_length) = if let Some(label) = step.strip_suffix('-') {
            (label, None)
        } else {
            let (label, focal_length) = step.split_once('=').unwrap();
            (label, Some(focal_length.parse().unwrap()))
        };

        let b = &mut boxes[usize::from(hash(label))];
        if let Some(existing_pos) = b.iter().position(|&(l, _fl)| l == label) {
            if let Some(fl) = focal_length {
                b[existing_pos].1 = fl;
            } else {
                b.remove(existing_pos);
            }
        } else if let Some(fl) = focal_length {
            b.push((label, fl));
        }
    }

    let focal_powers = boxes.into_iter().enumerate().flat_map(|(box_num, lenses)| {
        lenses
            .into_iter()
            .enumerate()
            .map(move |(lens_num, (_label, focal_length))| {
                (box_num + 1) * (lens_num + 1) * focal_length
            })
    });

    println!("{}", focal_powers.sum::<usize>());
}
