#!/usr/bin/perl use strict; my $x = ; chomp( $x ); my @d = split( / /, $x ); print readnode( )."\n"; exit; sub readnode { my $r=0; my $nc=shift( @d ); # Nos children my $nm=shift( @d ); # Nos metadata my @cnvals=(); # Keep track of values of child nodes for ( my $ct=0; $ct < $nc ; $ct++ ) { push( @cnvals, readnode() ); } my $r=0; my @mdvals=(); for ( my $ct=0; $ct < $nm ; $ct++ ) { my $x = shift( @d ); push( @mdvals, $x ); # keep track of metadata values $r+=$x; # keep tally of metadata values in case nos children is 0 } return( $r ) if( $nc == 0 ); # If no children then the value of this node is the sum of the metadata # Otherwise the metadata values are indexes to the children's node values $r=0; for ( my $ct=0; $ct < $nm ; $ct++ ) { $r+=$cnvals[$mdvals[$ct]-1]; # Abuse perl's lack of bounds checking and uninitialised array elements return 0 } return( $r ); }