#!/usr/bin/perl -w

use strict;

local $/ = "\n\n";

my @pl1 = <> =~ /^(\d+)$/gxms;
my @pl2 = <> =~ /^(\d+)$/gxms;
print "pl2=@pl2.\n";

while (@pl1 && @pl2) {
	my $p1 = shift @pl1;
	my $p2 = shift @pl2;
	print "$p1 vs $p2\n";
	if ($p1 > $p2) {
		push @pl1, $p1, $p2;
	} else {
		push @pl2, $p2, $p1;
	}
}

@pl1 = @pl2 if @pl2;
my $sum = 0;
while (@pl1) {
	$sum += +$pl1[0] * @pl1;
	shift @pl1;
}
print "sum=$sum\n";
