SUMGCD-help

https://www.codechef.com/submit/SUMAGCD
Here goes snippet, I got WA in two test cases

cin>>n;
        ll temp;
        set<ll>s;
        for(int i=0;i<n;++i){cin>>temp;s.insert(temp);}
        if(s.size()==1) cout<<2*(*max_element(all(s)))<<endl;
        else if(s.size()==2) cout<<*max_element(all(s))+*min_element(all(s))<<endl;
        else{
            auto it=s.rbegin();
            ll mx=*it;
            ll smx=*(--it);
            // cout<<mx<<" "<<smx;
            ll gc=0;
            for(auto it=s.begin();*it!=smx;++it){
                gc=__gcd(gc,*it);
            }
            // cout<<gc<<endl;
            ll a=__gcd(gc,mx)+smx;
            ll b=__gcd(gc,smx)+mx;
            cout<<max(a,b)<<endl;
        }