Optimal nonmyopic gambling strategy for the generalized Kelly criterion