Q-WSL: Optimizing Goal-Conditioned RL with Weighted Supervised Learning via Dynamic Programming