#!/usr/bin/perl -w

use v5.30;
use strict;

my ($g, $sleep_m);
my (%sleep_g, %sleep_g_m);
for (sort <>) {
	if (/#(\d+) begins/) {
		$g = $1;
	} elsif (/:(\d+)\] falls/) {
		$sleep_m = $1;
	} elsif (/:(\d+)\] wakes/) {
		$sleep_g{$g} += $1 - $sleep_m;
		for my $min ($sleep_m .. $1 - 1) {
			$sleep_g_m{$g}{$min}++;
		}
	}
}

my $max_g;
my $max_g_sleep;
for my $g (keys %sleep_g) {
	if (!$max_g_sleep || $max_g_sleep < $sleep_g{$g}) {
		$max_g = $g;
		$max_g_sleep = $sleep_g{$g};
	}
}
my $max_min;
my $max_min_sleep;
for my $min (keys %{ $sleep_g_m{$max_g} }) {
	my $n = $sleep_g_m{$max_g}{$min};
	if (!defined $max_min_sleep || $max_min_sleep < $n) {
		$max_min = $min;
		$max_min_sleep = $n;
	}
}

say $max_g * $max_min;
			
			

		
	

