algorithm for ACM