#!/usr/bin/env perl
use strict;
use warnings;

my $preamble = 25;
my $n = 0;
my @mem;
while (my $l = <ARGV>) {
    chomp $l;
    if ($n < $preamble) {
        # build memory
        push (@mem, $l);
        $n++;
    } else {
        # use memory
        check($l, \@mem);
        shift(@mem);
        push(@mem, $l);
    }
}
exit 1;

sub check {
    my ($l, $memA) = @_;

    # TODO: optimize?
    my %seen;
    for my $m (@$memA) {
        $seen{$m} = 1;
        my $c = $l - $m;
        if ($seen{$c} && $c != $m) {
            # success!
            return;
        }
    }

    # nothing adds up! report & exit
    print "$l\n";
    exit 0;
}
