Partition around medoids (PAM) algorithm: build phase (1 of 2).