protected final long costPerGroup(Int2IntCounterMap colocatedReplicaCounts) {
    final AtomicLong cost = new AtomicLong(0);
    // colocatedReplicaCounts is a sorted array of primary ids of regions. Replicas of regions
    // sharing the same primary will have consecutive numbers in the array.
    colocatedReplicaCounts.forEach((primary, count) -> {
      if (count > 1) { // means consecutive primaries, indicating co-location
        cost.getAndAdd((count - 1) * (count - 1));
      }
    });
    return cost.longValue();
  }
For each primary region, it computes the total number of replicas in the array (numReplicas)
and returns a sum of numReplicas-1 squared. For example, if the server hosts regions a, b, c,
d, e, f where a and b are same replicas, and c,d,e are same replicas, it returns (2-1) * (2-1)
+ (3-1) * (3-1) + (1-1) * (1-1).
@param colocatedReplicaCounts a sorted array of primary regions ids for the regions hosted
@return a sum of numReplicas-1 squared for each primary region in the group.
hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/balancer/RegionReplicaGroupingCostFunction.java