($n,$k)=glob<>; for(<>){ ($s,$p,$u)=glob; push@{$a[$s]{$u}//=[]},$p*2e5+$i; ++$i; } for(reverse@a){ for$u(keys%$_){ @{$_->{$u}}=sort{$b-$a}@{$_->{$u}}; } $m=0; while(1){ $w=0; for$u(keys%$_){ if(@{$_->{$u}}){ if($v{$u}<$m){ push@b,pop@{$_->{$u}}; ++$v{$u}; }else{ $w=1; } } } ++$m,redo if !@b && $w; last if !@b; @b=sort{$b-$a}@b; while(@b){ print pop(@b)%2e5,$/; exit if !--$k; } } }