Approach hintTry it first, then revealHide
Fix the divisor a[j] and walk over its multiples m; the best remainder just below m comes from the largest element smaller than m. Precompute that element for every value so the harmonic loop stays fast.