#!/usr/bin/perl -w

use v5.16;
use strict;

my (%speed, %time, %resttime);
my %state;
my %dist;
while (<>) {
	my ($name, $sp, $t, $r) = /(\w+) can fly (\d+) .* for (\d+) .*rest for (\d+) /;
	$speed{$name} = $sp;
	$time{$name} = $t;
	$resttime{$name} = $r;
	$state{$name} = $t;
};


my $time = 2503;

my $max;

for my $n (keys %speed) {
	my $loops = int($time/($time{$n}+$resttime{$n}));
	my $dist = $loops * $time{$n} * $speed{$n};
	my $rest = $time - $loops*($time{$n}+$resttime{$n});
	$rest = $time{$n} if $rest > $time{$n};
	$dist += $speed{$n} * $rest;
	say "$n $dist";
	$max = $dist if !$max || $max < $dist;
}

say $max;
		
