CF - Orac & LCM

I’m currently stuck on understanding the solution to this problem, and I would appreciate any help!

My understanding is that the solution can rewrite the given condition to lcm(a[j], gcd(a[i])) since GCD/LCM operations distribute over each other. The part I don’t understand is how “Taking the GCD of the results for each j gives the answer.” But although this doesn’t seem to match the rewritten condition, my code indeed passes:

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n; cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    long long g = 0, ans = 0;
    for (int i = 0; i < n; ++i) {
        ans = gcd(ans, lcm(a[i], g));
        g = gcd(g, a[i]);
    cout << ans;