#!/usr/bin/perl -a
use 5.10.0;
@a=($F[-1]-$b?$b:2*pop@F,@a)while$b=pop@F;say"@a"