#!/usr/bin/perl -w

use v5.30;
use strict;

my @tree = split /\s+/, <>;

use List::Util qw(sum);

my $metasum = 0;
sub walk {
	my $subnodes = shift @tree;
	my $metadata = shift @tree;
	say "walk $subnodes $metadata";
	while ($subnodes--) {
		walk();
	}
	while ($metadata--) {
		$metasum += shift @tree;
	}
}
walk();
say $metasum;
	

