medium calculate solution