make stack water = 0 for n in columns while stack not empty and n > stack top water += min(stack bottom, n) - stack top pop stack stack push n