#!/usr/bin/perl -w

use v5.36;
use strict;
use experimental 'multidimensional';
use List::Util qw(sum);

my @path = '';
my %files;
my %dirs = ('' => 0);

while (<>) {
	chomp;
	if (/^\$ cd \/$/) {
		@path = '';
	} elsif (/^\$ cd \.\.$/) {
		pop @path;
	} elsif (/^\$ cd (.*)/) {
		push @path, $1;
	} elsif (/^\$ ls/) {
	} elsif (/^dir (.*)/) {
		$dirs{join('/', @path, $1)} = 0;
	} elsif (/^(\d+) (.*)/) {
		my $file = join('/', @path, $2);
		$files{$file} = $1;
	}
}

for my $dir (keys %dirs) {
	for my $file (keys %files) {
		$dirs{$dir} += $files{$file} if $file =~ /^$dir\//;
	}
}

my $reqd = $dirs{''} - 40000000;

for my $dir (sort { $dirs{$a} <=> $dirs{$b} } keys %dirs) {
	if ($dirs{$dir} >= $reqd) {
		say $dir, ' ', $dirs{$dir};
		last;
	}
}
	
