fixed query1 to work with optimized way with the sorted Weights
added check for weights in the for loop for fast exit