how to select only top faces of multiple boxes which grouped together

Hello everyone,
i have a lot of boxes group as a building(every single box like a brick), the problem i am trying to figure out is i want to select all top faces of this building. Becasue there must be some box overlap on another, and the top of the building actual not flat, so it cannot be achieved by simply calculate the shortest distance between the facecenter and the max.z of bounding box.
How can i know which box is the topmost and then select the topface of these boxes? Anyone can help to give me some script examples or is there any existing tools can do it ?