IxlOA #1
Identical Distribution
EasyJudge-ready
Topics (AI-labelled)Bits & Math
There are several types of cards, and cardTypes[i] is the number of cards of type i. You may add cards of existing types and choose a number of packs greater than 1 so that every pack contains the same number of cards of each type. Find the minimum number of cards to add.
Input
The first line contains n, and the second line contains n positive integers cardTypes. Ranges: 1≤n≤10⁵ (original constraint), 1≤cardTypes[i]≤1000 (the original gives no upper bound on values; this is this site's OJ limit).
Output
Output the minimum total number of cards to add.
Example 1
Input:
5
4 7 5 11 15
Output:
4
Example 2
Input:
1
1
Output:
1
Example 3
Input:
1
2
Output:
0
Example notes
For each possible number of packs, compute how many cards each type is short of the next multiple; enumerating pack counts up to the largest existing count plus 1 is enough to cover the optimal solution.
Editorial
Loading solution…