Quantization based on dynamic programming