30 units possible to carry 1 unit -> 1 cell start with 30 unit of gas ( 0 30) go forward 10 put 10 go back with remainig 10 units ( 0 0) go forward with 30 again spent 10 but get 10 (10 30) go forward 6 spending 6 put 8 (16 16) go back to the base spending remaining 16 units ( 0 0) go forward 16 with 30 but spend 16 but get 8 (16 22) then go forward and reach 38 units Note that parameters here are highly artificially devised to fit the problem. ===