#!/usr/bin/perl -w

use strict;

my @prev;

my $target = 375054920;

my $sum = 0;
while (my $num = <>) {
	chomp $num;

	$sum += $num;
	push @prev, $num;
	
	print "Adding $num, sum=$sum\n";

	while ($sum > $target) {
		my $n1 = shift @prev;
		$sum -= $n1;
		print "Removing $n1, sum=$sum\n";
	}

	if ($sum == $target) {
		print "found $sum = ", join('+', @prev), "\n";
		my ($min, $max);
		for my $n1 (@prev) {
			$min = $n1 if !defined $min || $min > $n1;
			$max = $n1 if !defined $max || $max < $n1;
		}
		print "$min+$max=", $min+$max, "\n";
		last;
	}
}

