function m = dynam(v)
% Recursive solution to max value of non-adjacent elements in 2 rows
% without advantage of dynamic programming

[r n] = size(v);
m = f1(n);

   %------------------- Best if assume both at n-1 unused  --------
   function b = f1(n)
     if n == 0 
        b = 0;
     else 
        b = max([f1(n-1), v(1,n)+f2(n-1), v(2,n)+f3(n-1)]);
        % Best of neither, add top value, add bottom value
     end
   end

   %------------------- Best if assume top at n-1 unused ----------
   function b = f2(n)
     if n == 0 
        b = 0;
     else
        b = max([f1(n-1), v(2,n)+f3(n-1)]);
        % Best of neither, add top value
     end
   end

   %------------------- Best if assume bottom at n-1 unused -------
   function b = f3(n)
     if n == 0
        b = 0;
     else 
        b = max([f1(n-1), v(1,n)+f2(n-1)]);
        % Best of neither, add bottom value
     end
   end
end

