6
int sumOfFlooredPairs(vector<int> &nums) {7
// first of all, we record the max value9
for (int n : nums) max_n = max(max_n, n);11
// then the occurences for each number in [0, max]12
vector<int> occs(max_n + 1, 0);13
for (int n : nums) occs[n]++;15
// prefix sum algorithm to accumulate the occurences16
vector<int> occs_acc(max_n + 1, 0);17
for (int i = 1; i < max_n + 1; ++i) {18
occs_acc[i] = occs[i] + occs_acc[i - 1];21
// long long needed to prevent overflows23
for (int i = 0; i < max_n + 1; ++i) {24
// just handle numbers that occur at least once29
// for each multiple of i33
// "right and left" multipliers in occs_acc34
int r = min(k_next * i - 1, max_n);37
ans += ((long long)occs_acc[r] - (long long)occs_acc[l]) * (long long)occs[i] * k++;39
} while (k_next * i - 1 < max_n);