#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for(int i=0; i<int(n); i++)
#define RNG(i, f, t) for(int i=int(f); i<=int(t); i++)
bool RELAXED = false;
template<int,typename T> struct MINMAX{ T val; MINMAX(T val) : val(val) {} };
template<typename T> MINMAX<1,T> MAX(T val) { return MINMAX<1,T>(val); };
template<typename T> MINMAX<2,T> MIN(T val) { return MINMAX<2,T>(val); };
template<typename T, typename U> inline T& operator|=(T &lhs, MINMAX<1,U> rhs) { return lhs = (rhs.val > lhs ? (RELAXED=true,rhs.val) : (RELAXED=false,lhs)); }
template<typename T, typename U> inline T& operator|=(T &lhs, MINMAX<2,U> rhs) { return lhs = (rhs.val < lhs ? (RELAXED=true,rhs.val) : (RELAXED=false,lhs)); }
template<typename T> inline vector<T> readVector(int n) { vector<T> vec(n); FOR(i,n) cin>>vec[i]; return vec; }
template<typename T> void EXIT(T answer) { cout<<answer<<endl; exit(0); }
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef signed long long int ll;

const int MAXN = 5e5+5;
vector<vpii> adj(MAXN);
vector<vector<ll>> dp(MAXN, vector<ll>(2));
int n,k;

ll dfs(int u, int p, int kk) {
    if(dp[u][k-kk] >= 0) return dp[u][k-kk];

    ll sum=0;
    vector<ll> extra;
    for(auto to : adj[u]) {
        if(to.first==p) continue;
        ll nosat = dfs(to.first, u, k);
        ll sat = dfs(to.first, u, k-1) + to.second;
        sum += nosat;
        extra.push_back(sat - nosat);
    }
    sort(extra.rbegin(), extra.rend());
    FOR(i,min(kk,(int)extra.size())) {
        if(extra[i] > 0)
            sum += extra[i];
    }

    return dp[u][k-kk] = sum;
}

void solve() {
    cin>>n>>k;
    FOR(i,n-1) {
        int a,b,w;
        cin>>a>>b>>w;
        adj[a].push_back({b,w});
        adj[b].push_back({a,w});
    }

    ll ans = dfs(1,-1,k);
    cout<<ans<<endl;

    FOR(i,n+1) adj[i].clear(), dp[i] = {-1LL, -1LL};
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

    FOR(i,MAXN) dp[i] = {-1LL, -1LL};

    int T; cin>>T;
    while(T--) solve();
    return 0;
}


///Ma-ia-hii
///Ma-ia-huu
///Ma-ia-hoo
///Ma-ia-haa
///X4

///Alo, Salut, Sunt Eu, un Haiduc
///Si Te Rog, Iubirea mea Primeste Fericirea
///Alo, Alo, Sunt Eu, Picasso
///Ti-am dat beep
///Si Sunt Voinic
///Dar Sa Stii Nu-ti Cer Nimic

///Vrei sa pleci dar nu ma, nu ma iei
///nu ma, nu ma iei, nu ma, nu ma, nu ma iei
///Nu Ma, Nu Ma Iei, nu ma, nu ma, nu ma iei
///Chipul tau si dragostea din tei
///Mi-amintesc de ochii tai

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define DIM (long long)500007
#define INF (long long)1000000007
#define fi first
#define se second
#define x1 sfhshfd
#define y1 sfhs321321hfd
#define glhf ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define GG cout << " OPANA " << endl;

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pairll;
typedef tree<ll,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> SATANA;

ll q,n,k;
vector< pairll > a[DIM];
ll dp1[DIM];
ll dp2[DIM];
ll used[DIM];
ll d[DIM];

void dfs(ll v, ll w){
    used[v]=1;
    set< pair< pairll , ll > >s1;
    queue< ll > q;
    if(a[v].size()==1 && v!=1){
        dp1[v]=w;
        dp2[v]=0;
        return;
    }

    ll nowk=0;
    ll nk=min(k,(ll)a[v].size()-1);
    if(v==1) nk++;
    nk=min(nk,k);

    ll res=0;
    for(auto qqq:a[v])
    {
        ll to=qqq.first;
        ll w=qqq.second;
        if(used[to]==1) continue;
        dfs(to,w);

        if(dp1[to]>dp2[to]) {
            s1.insert({{dp1[to]-dp2[to],dp1[to]},to});
        }
        else {
            res+=dp2[to];
            d[to]=1;
        }
        q.push(to);
    }

    ///1 - NEBEREM
    ll last=0;
    ll i=1;
    for(;i<=nk; i++){

        if(s1.empty()) break;
        auto qqqq=*s1.rbegin();
        last=(qqqq.first.second);

        d[qqqq.second]=1;

        res+=last;

        s1.erase(qqqq);

    }

    while(q.size()){
        ll z=q.front();
        if(d[z]==0) res+=dp2[z];
        //if(v==1) cout << "AAAA " << res << " " << z << " " << d[z] << endl;
        q.pop();
    }
   // if(v==1) cout << " OPA " << nk << " " << res << " " << last << " " << w << " " << i << endl;
    dp2[v]=res;
    dp1[v]=res-last+w;
    if(s1.empty() && i!=nk+1) dp1[v]+=last;

}
int main()
{

    glhf;
    ll v1,v2,w;
    cin >> q;
    while(q--){
        cin >> n >> k;
        ll res=0;
        for(int i=1; i<n; i++){

            cin >> v1 >> v2 >> w;
            a[v1].push_back({v2,w});
            a[v2].push_back({v1,w});
        }

        dfs(1,0);
        cout << max(dp1[1],dp2[1]) << endl;
        for(int i=1; i<=n; i++) {
            a[i].clear();
            used[i]=0;
            //cout << i << " " << dp1[i] << " " << dp2[i] << endl;
            dp1[i]=0;
            dp2[i]=0;

        }
    }

    return 0;
}


///Ma-ia-hii
///Ma-ia-huu
///Ma-ia-hoo
///Ma-ia-haa
///X4

///Alo, Salut, Sunt Eu, un Haiduc
///Si Te Rog, Iubirea mea Primeste Fericirea
///Alo, Alo, Sunt Eu, Picasso
///Ti-am dat beep
///Si Sunt Voinic
///Dar Sa Stii Nu-ti Cer Nimic

///Vrei sa pleci dar nu ma, nu ma iei
///nu ma, nu ma iei, nu ma, nu ma, nu ma iei
///Nu Ma, Nu Ma Iei, nu ma, nu ma, nu ma iei
///Chipul tau si dragostea din tei
///Mi-amintesc de ochii tai

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define DIM (long long)500007
#define INF (long long)1000000007
#define fi first
#define se second
#define x1 sfhshfd
#define y1 sfhs321321hfd
#define glhf ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define GG cout << " OPANA " << endl;

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pairll;
typedef tree<ll,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> SATANA;

ll n,res,q,k;
ll dp[DIM][2];
vector< pairll > a[DIM];

void dfs(ll v, ll p){
    vector<ll> d;
    for(auto s:a[v]){
        ll to=s.first;
        ll w=s.second;
        if(to==p) continue;
        dfs(to,v);
        dp[v][0]+=dp[to][0];
        dp[v][1]+=dp[to][0];
        d.push_back(dp[to][1]-dp[to][0]+w);
    }

    sort(d.begin(),d.end());
    reverse(d.begin(),d.end());
    for(int i=0; i<min(k,(ll)d.size()); i++){
        if(d[i]<=0)  break;

        dp[v][0]+=d[i];
        if(i!=k-1) dp[v][1]+=d[i];
    }
}
void clr(){
    for(int i=1; i<=n; i++) {
        dp[i][0]=0;dp[i][1]=0;
        a[i].clear();
    }
}
int main()
{
    glhf;
    cin >> q;
    while(q--){
        cin >> n >> k;
        ll v1,v2,w;
        for(int i=1; i<n; i++){
            cin >> v1 >> v2 >> w;
            a[v1].push_back({v2,w});
            a[v2].push_back({v1,w});
        }
        dfs(1,0);
        cout << dp[1][0] << endl;
        clr();
    }

    return 0;
}


///Ma-ia-hii
///Ma-ia-huu
///Ma-ia-hoo
///Ma-ia-haa
///X4

///Alo, Salut, Sunt Eu, un Haiduc
///Si Te Rog, Iubirea mea Primeste Fericirea
///Alo, Alo, Sunt Eu, Picasso
///Ti-am dat beep
///Si Sunt Voinic
///Dar Sa Stii Nu-ti Cer Nimic

///Vrei sa pleci dar nu ma, nu ma iei
///nu ma, nu ma iei, nu ma, nu ma, nu ma iei
///Nu Ma, Nu Ma Iei, nu ma, nu ma, nu ma iei
///Chipul tau si dragostea din tei
///Mi-amintesc de ochii tai

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define DIM (long long)500007
#define INF (long long)1000000007
#define fi first
#define se second
#define x1 sfhshfd
#define y1 sfhs321321hfd
#define glhf ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define GG cout << " OPANA " << endl;

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pairll;
typedef tree<ll,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> SATANA;

ll q,n,k;
vector< pairll > a[DIM];
ll dp1[DIM];
ll dp2[DIM];
ll used[DIM];
ll d[DIM];

void dfs(ll v, ll w){
    used[v]=1;
    set< pair< pairll , ll > >s1;
    queue< ll > q;
    if(a[v].size()==1 && v!=1){
        dp1[v]=w;
        dp2[v]=0;
        return;
    }

    ll nowk=0;
    ll nk=k;
    if(v==1) nk++;
    nk=min(nk,k);

    ll res=0;
    for(auto qqq:a[v])
    {
        ll to=qqq.first;
        ll w=qqq.second;
        if(used[to]==1) continue;
        dfs(to,w);

        if(dp1[to]>dp2[to]) {
            s1.insert({{dp1[to]-dp2[to],dp1[to]},to});
        }

        q.push(to);
    }

    ll last=0;
    ll i=1;
    //if(v==1) cout << "opa " << i << " " << nk << " " << res <<  endl;
    for(;i<=nk; i++){

        if(s1.empty()) break;
        auto qqqq=*s1.rbegin();
        last=(qqqq.first.second);

        d[qqqq.second]=1;
        //if(v==1) cout << " AAA " << qqqq.second << endl;

        res+=last;

        s1.erase(qqqq);

    }

   // if(v==1) cout << "opa " << i << " " << nk << " " << res <<  endl;
    while(q.size()){
        ll z=q.front();
       // if(v==1) cout << z << " " << d[z] << " " << dp2[z] <<  endl;
        if(d[z]==0) res+=dp2[z];
        //if(v==1) cout << "AAAA " << res << " " << z << " " << d[z] << endl;
        q.pop();
    }
   // if(v==1) cout << " OPA " << nk << " " << res << " " << last << " " << w << " " << i << endl;
   //if(v==1) cout << "opa " << i << " " << nk << " " << res <<  endl;
    dp2[v]=res;
    dp1[v]=res-last+w;
    if(s1.empty() && i!=nk+1) dp1[v]+=last;

}
int main()
{

    glhf;
    ll v1,v2,w;
    cin >> q;
    while(q--){
        cin >> n >> k;
        ll res=0;
        for(int i=1; i<n; i++){

            cin >> v1 >> v2 >> w;
            a[v1].push_back({v2,w});
            a[v2].push_back({v1,w});
        }

        dfs(1,0);
        cout << dp2[1] << endl;
        for(int i=1; i<=n; i++) {
            a[i].clear();
            used[i]=0;
            //cout << i << " " << dp1[i] << " " << dp2[i] << endl;
            dp1[i]=0;
            dp2[i]=0;
            d[i]=0;

        }
    }

    return 0;
}


///Ma-ia-hii
///Ma-ia-huu
///Ma-ia-hoo
///Ma-ia-haa
///X4

///Alo, Salut, Sunt Eu, un Haiduc
///Si Te Rog, Iubirea mea Primeste Fericirea
///Alo, Alo, Sunt Eu, Picasso
///Ti-am dat beep
///Si Sunt Voinic
///Dar Sa Stii Nu-ti Cer Nimic

///Vrei sa pleci dar nu ma, nu ma iei
///nu ma, nu ma iei, nu ma, nu ma, nu ma iei
///Nu Ma, Nu Ma Iei, nu ma, nu ma, nu ma iei
///Chipul tau si dragostea din tei
///Mi-amintesc de ochii tai

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define DIM (long long)500007
#define INF (long long)1000000007
#define fi first
#define se second
#define x1 sfhshfd
#define y1 sfhs321321hfd
#define glhf ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define GG cout << " OPANA " << endl;

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pairll;
typedef tree<ll,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> SATANA;

ll q,n;
struct ev{
    ll v1,v2,w;
};
ev a[DIM];
ll d[DIM];

bool cmp(ev t1, ev t2){
    return t1.w<t2.w;
}
int main()
{

    glhf;
    ll v1,v2;
    cin >> q;
    ll k;
    while(q--){
        cin >> n >> k;
        ll res=0;
        for(int i=1; i<n; i++) cin >> a[i].v1 >> a[i].v2 >> a[i].w;
        for(int i=1; i<=n; i++) d[i]=0;
        sort(a+1,a+n,cmp);

        for(int i=n-1; i>=1; i--){

            v1=a[i].v1;
            v2=a[i].v2;
        //cout << v1 << v2 << a[i].w << endl;
            if(d[v1]<k && d[v2]<k) {
                d[v1]++;
                d[v2]++;
                res+=a[i].w;
            }
        }
        cout << res << endl;
    }

    return 0;
}


///Ma-ia-hii
///Ma-ia-huu
///Ma-ia-hoo
///Ma-ia-haa
///X4

///Alo, Salut, Sunt Eu, un Haiduc
///Si Te Rog, Iubirea mea Primeste Fericirea
///Alo, Alo, Sunt Eu, Picasso
///Ti-am dat beep
///Si Sunt Voinic
///Dar Sa Stii Nu-ti Cer Nimic

///Vrei sa pleci dar nu ma, nu ma iei
///nu ma, nu ma iei, nu ma, nu ma, nu ma iei
///Nu Ma, Nu Ma Iei, nu ma, nu ma, nu ma iei
///Chipul tau si dragostea din tei
///Mi-amintesc de ochii tai

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define DIM (long long)500007
#define INF (long long)1000000007
#define fi first
#define se second
#define x1 sfhshfd
#define y1 sfhs321321hfd
#define glhf ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define GG cout << " OPANA " << endl;

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pairll;
typedef tree<ll,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> SATANA;

ll q,n,k;
vector< pairll > a[DIM];
ll dp1[DIM];
ll dp2[DIM];
ll used[DIM];
ll d[DIM];

void dfs(ll v, ll w){
    used[v]=1;
    set<ll> s1;
    if(a[v].size()==1){
        dp1[v]=w;
        dp2[v]=0;
        return;
    }
    ll nowk=0;
    ll nk=min(k,(ll)a[v].size()-(ll)1);

    ll res=0;
    for(auto qqq:a[v])
    {
        ll to=qqq.first;
        ll w=qqq.second;
        if(used[to]==1) continue;
        dfs(to,w);

        if(dp1[to]>dp2[to]) {
            s1.insert(dp1[to]);
        }
        else {
            res+=dp2[to];
        }
    }

    ///1 - NEBEREM
    ll last=0;
    for(int i=1; i<=nk; i++){
        if(s1.empty()) break;
        last=(*s1.rbegin());
        res+=last;
        s1.erase((*s1.rbegin()));

    }

    dp2[v]=res;
    dp1[v]=res-last+w;

}
int main()
{

    glhf;
    ll v1,v2,w;
    cin >> q;
    while(q--){
        cin >> n >> k;
        ll res=0;
        for(int i=1; i<n; i++){

            cin >> v1 >> v2 >> w;
            a[v1].push_back({v2,w});
            a[v2].push_back({v1,w});
        }

        dfs(1,0);
        cout << max(dp1[1],dp2[1]) << endl;
        for(int i=1; i<=n; i++) {
            a[i].clear();
            used[i]=0;
            //cout << dp1[i] << " " << dp2[i] << endl;
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define vll vector<LL>
#define pii pair<int,int>
#define fr first
#define sc second
#define  LL   long long int
#define inf 1<<29
#define mod 4294967296
#define LSOne(a) (a&(-a))
#define maxL  50010
#define REP(a,b) for(int a=0;a<b;a++)
class ST{
public :
    vi fix;
    vll Tree;
    vi mx,mn;
    int n;
    void recover(int a,int L,int R)
    {
        if(fix[a]!=-1)
           {
             fix[a*2 ]=fix[a*2 +1]=fix[a];
             mx[a*2 ]=mx[a*2 +1]=mx[a];
             mn[a*2 ]=mn[a*2 +1]=mn[a];
             Tree[a*2]=1ll*fix[a*2]*1ll*((R+L)/2 -L +1);
             Tree[a*2 +1]=1ll*fix[a*2 +1]*1ll*(R - (R+L)/2 );
           }
            return;
    }
    void update(int p,int L,int R,int i,int j,int x) // O(lg n )
    {
        if(L>j   ||  R<i || mn[p]>=x)
                return;
        if(L>=i   && R<=j && mx[p]<=x)
        {
            fix[p]=mx[p]=mn[p]=x;
            Tree[p]=(1ll*x*1ll*(R-L+1));
            return;
        }
        recover(p,L,R);
        fix[p]=-1;
        update(p<<1,L,(L+R)>>1,i,j,x);
        update((p<<1)   +1,((L+R)>>1)  +1,R,i,j,x);
        Tree[p]=Tree[p<<1]+Tree[(p<<1)  +1];
        mx[p]=max(mx[p<<1],mx[p*2  +1]);
        mn[p]=min(mn[p<<1],mn[p*2  +1]);
        return;
    }
    ST(int a)
    {
        n=a;
        Tree.assign(4*n,0);
        fix.assign(4*n,-1);
        mx.assign(4*n,0);
        mn.assign(4*n,0);
    }
    void update(int i,int j,int x)
    {
        update(1,0,n-1,i,j,x);
    }
};

    int L1[200010];
    int R1[200010];
    int L2[200010];
    int R2[200010];
void insrt(int a,int i)
{
    if(!L1[a])
        L1[a]=i;
    else if(!L2[a])
        L2[a]=i;
    R2[a]=R1[a];
    R1[a]=i;
    return ;
}

    LL H[200010];
int main()
{
    int n;
    scanf("%d",&n);
    int A[n];
    for(int i=0;i<n;i++)
    {
        scanf("%d",&A[i]);
        for(int j=1;j*j  <= A[i];j++)
            if(A[i]%j==0)
        {
            insrt(j,i+1);
            if(j*j <A[i])
                insrt(A[i]/j,i+1);
        }
    }
    ST B(n+1);
    for(int i=1;i<=n;i++)
    B.update(i,i,i);
    for(int i=200001;i>=1;i--)
    {
        if(L1[i]!=R1[i])
        {
            B.update(L1[i]+1,L2[i],R1[i]);
            B.update(L2[i]+1,n,n+1);
            B.update(1,L1[i],R2[i]);
        }
        H[i]=(1ll*n)*(1ll*(n+1))    -B.Tree[1];
    }
    LL ans=0;
    for(int i=1;i<200001;i++)
        ans+=(H[i+1]-H[i])*(1ll*i);
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define vll vector<LL>
#define pii pair<int,int>
#define fr first
#define sc second
#define  LL   long long int
#define inf 1<<29
#define mod 4294967296
#define LSOne(a) (a&(-a))
#define maxL  50010
#define REP(a,b) for(int a=0;a<b;a++)
class ST{
public :
    vi fix;
    vll Tree;
    vi mx,mn;
    int n;
    void recover(int a,int L,int R)
    {
        if(fix[a]!=-1)
           {
             fix[a*2 ]=fix[a*2 +1]=fix[a];
             mx[a*2 ]=mx[a*2 +1]=mx[a];
             mn[a*2 ]=mn[a*2 +1]=mn[a];
             Tree[a*2]=1ll*fix[a*2]*1ll*((R+L)/2 -L +1);
             Tree[a*2 +1]=1ll*fix[a*2 +1]*1ll*(R - (R+L)/2 );
           }
            return;
    }
    void update(int p,int L,int R,int i,int j,int x) // O(lg n )
    {
        if(L>j   ||  R<i || mn[p]>=x)
                return;
        if(L>=i   && R<=j && mx[p]<=x)
        {
            fix[p]=mx[p]=mn[p]=x;
            Tree[p]=(x*(R-L+1));
            return;
        }
        recover(p,L,R);
        fix[p]=-1;
        update(p<<1,L,(L+R)>>1,i,j,x);
        update((p<<1)   +1,((L+R)>>1)  +1,R,i,j,x);
        Tree[p]=Tree[p<<1]+Tree[(p<<1)  +1];
        mx[p]=max(mx[p<<1],mx[p*2  +1]);
        mn[p]=min(mn[p<<1],mn[p*2  +1]);
        return;
    }
    ST(int a)
    {
        n=a;
        Tree.assign(4*n,0);
        fix.assign(4*n,-1);
        mx.assign(4*n,0);
        mn.assign(4*n,0);
    }
    void update(int i,int j,int x)
    {
        update(1,0,n-1,i,j,x);
    }
};

    int L1[200010];
    int R1[200010];
    int L2[200010];
    int R2[200010];
void insrt(int a,int i)
{
    if(!L1[a])
        L1[a]=i;
    else if(!L2[a])
        L2[a]=i;
    R2[a]=R1[a];
    R1[a]=i;
    return ;
}

    LL H[200010];
int main()
{
    int n;
    scanf("%d",&n);
    int A[n];
    for(int i=0;i<n;i++)
    {
        scanf("%d",&A[i]);
        for(int j=1;j*j  <= A[i];j++)
            if(A[i]%j==0)
        {
            insrt(j,i+1);
            if(j*j <A[i])
                insrt(A[i]/j,i+1);
        }
    }
    ST B(n+1);
    for(int i=1;i<=n;i++)
    B.update(i,i,i);
    for(int i=200001;i>=1;i--)
    {
        if(L1[i]!=R1[i])
        {
            B.update(L1[i]+1,L2[i],R1[i]);
            B.update(L2[i]+1,n,n+1);
            B.update(1,L1[i],R2[i]);
        }
        H[i]=(1ll*n)*(1ll*(n+1))    -B.Tree[1];
    }
    LL ans=0;
    for(int i=1;i<200001;i++)
        ans+=(H[i+1]-H[i])*i;
    cout<<ans<<endl;
    return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long LL;
const int maxn=2E5+77;

char _mem[30000007];
int _cnt;
inline void *alloc(const size_t &size){
	void *ret=_mem+_cnt;
	_cnt+=size;
	return ret;
}

int N,A[maxn],P[maxn];
LL H[maxn];

struct SEG{
	SEG *ch[2];
	int assign,mn;
	LL sum;
	void *operator new(size_t size){return alloc(size);}
	void pass(int L,int R){
		if(assign){
			int M=L+((R-L)>>1);
			LL len;
			for(int i=0;i<2;++i){
				if(i==0) len=M-L+1;
				else len=R-M;
				ch[i]->assign=ch[i]->mn=assign;
				ch[i]->sum=len*assign;
			}
		}
		assign=0;
	}
	void maintain(){
		sum=ch[0]->sum+ch[1]->sum;
		mn=min(ch[0]->mn,ch[1]->mn);
	}
}*tree=NULL;
void segBuild(SEG *&tr=tree,int L=1,int R=N){
	tr=new SEG;
	if(L==R){
		tr->assign=tr->mn=tr->sum=L;
		return;
	}
	int M=L+((R-L)>>1);
	segBuild(tr->ch[0],L,M);
	segBuild(tr->ch[1],M+1,R);
	tr->maintain();
}
void segSet(SEG *tr,int L,int R,int qL,int qR,int v){
	if(L==qL && R==qR){
		tr->assign=tr->mn=v;
		tr->sum=1ll*v*(R-L+1);
		return;
	}
	tr->pass(L,R);
	int M=L+((R-L)>>1);
	if(qR<=M) segSet(tr->ch[0],L,M,qL,qR,v);
	else if(qL>M) segSet(tr->ch[1],M+1,R,qL,qR,v);
	else segSet(tr->ch[0],L,M,qL,M,v),segSet(tr->ch[1],M+1,R,M+1,qR,v);
	tr->maintain();
}
int segQuery(SEG *tr,int L,int R,int v){
	if(L==R){
		if(tr->mn<v) return L;
		else return -1;
	}
	tr->pass(L,R);
	int M=L+((R-L)>>1);
	if(tr->ch[1]->mn<v) return segQuery(tr->ch[1],M+1,R,v);
	else return segQuery(tr->ch[0],L,M,v);
}

vector<int> D[maxn];
int V[maxn],cv=0;

int main(){
	scanf("%d",&N);
	for(int i=1;i<=N;++i) scanf("%d",A+i),P[A[i]]=i;
	segBuild();
	//cerr<<"tree-sum:"<<tree->sum<<endl;
	for(int i=1;i<maxn;++i){
		for(int j=i;j<maxn;j+=i) if(P[j]) D[i].push_back(P[j]);
		sort(D[i].begin(),D[i].end());
	}
	for(int i=maxn-1;i>=1;--i){
		if(D[i].size()>=2){
			if(D[i][1]<N){
				segSet(tree,1,N,D[i][1]+1,N,N+1);
				//cerr<<"segSet "<<D[i][1]+1<<" "<<N<<":"<<N+1<<endl;
			}
			int p=segQuery(tree,1,N,*D[i].rbegin());
			if(p>D[i][0]){
				segSet(tree,1,N,D[i][0]+1,min(p,D[i][1]),*D[i].rbegin());
				//cerr<<"segset2 "<<D[i][0]+1<<" "<<min(p,D[i][1])<<":"<<*D[i].rbegin()<<endl;
			}
			//cout<<"1"<<endl;
			p=segQuery(tree,1,N,*(D[i].rbegin()+1));
			//cout<<"p:"<<p<<endl;
			if(p>=1){
				segSet(tree,1,N,1,min(p,D[i][0]),*(D[i].rbegin()+1));
				//cerr<<"segset3 "<<1<<" "<<min(p,D[i][0])<<":"<<*(D[i].rbegin()+1)<<endl;
			}
			H[i]=(1ll*N*(N+1))-tree->sum;
			//cerr<<"cur sum:"<<tree->sum<<endl;
			//cerr<<"H["<<i<<"]="<<H[i]<<endl;
			V[cv++]=i;
		}
	}
	LL tot=1ll*N*(N+1)>>1,ans=0;
	for(int i=0;i<cv;++i){
		LL v=V[i],cnt;
		if(!i) cnt=tot-H[v];
		else cnt=H[V[i-1]]-H[v];
		ans+=v*cnt;
	}
	printf("%I64d\n",ans);
}


#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
#define N 200000 + 5
#define M 200000
#define D 524288 + 5
#define E 5000000 + 5
#define ls(x) x << 1
#define rs(x) x << 1 | 1

int n, tot, Head[N], Min[N][2], Max[N][2], Point[N << 2][2];
LL ans;

struct Edge
{
	int next, node;
}h[E];

struct Segment_Tree
{
	int Min, del;
	LL Sum;
}Tree[D];

inline void addedge(int u, int v)
{
	h[++ tot].next = Head[u], Head[u] = tot;
	h[tot].node = v;
}

inline void Prepare()
{
	for (int i = 1; i <= M; i ++)
		for (int j = i; j <= M; j += i)
			addedge(j, i);
}

inline void Update(int u, int pos)
{
	if (!Min[u][0]) Min[u][0] = pos;
	else if (!Min[u][1]) Min[u][1] = pos;
	Max[u][1] = Max[u][0], Max[u][0] = pos;
}

inline void update(int x)
{
	Tree[x].Sum = Tree[ls(x)].Sum + Tree[rs(x)].Sum;
	Tree[x].Min = Tree[ls(x)].Min;
}

inline void Build(int x, int l, int r)
{
	Tree[x].del = 0;
	Tree[x].Sum = 1LL * (l + r - 2) * (r - l + 1) / 2;
	Tree[x].Min = l - 1;
	if (l == r) return ;
	int mid = l + r >> 1;
	Build(ls(x), l, mid);
	Build(rs(x), mid + 1, r);
	update(x);
}

inline int Find(int x, int l, int r, int k)
{
	if (l == r) return l;
	int mid = l + r >> 1;
	if (Tree[rs(x)].Min >= k)
		return Find(ls(x), l, mid, k);
	else return Find(rs(x), mid + 1, r, k);
}

inline void apply(int x, int l, int r, int k)
{
	Tree[x].Sum = 1LL * (r - l + 1) * k;
	Tree[x].Min = k;
	Tree[x].del = k;
}

inline void push(int x, int l, int r)
{
	if (Tree[x].del)
	{
		int mid = l + r >> 1;
		apply(ls(x), l, mid, Tree[x].del);
		apply(rs(x), mid + 1, r, Tree[x].del);
		Tree[x].del = 0;
	}
}

inline void Modify(int x, int l, int r, int s, int t, int k)
{
	if (l == s && r == t)
	{
		apply(x, l, r, k);
		return ;
	}
	push(x, l, r);
	int mid = l + r >> 1;
	if (t <= mid) Modify(ls(x), l, mid, s, t, k);
	else if (s > mid) Modify(rs(x), mid + 1, r, s, t, k);
	else Modify(ls(x), l, mid, s, mid, k), Modify(rs(x), mid + 1, r, mid + 1, t, k);
	update(x);
}

inline LL Query(int x, int l, int r, int s, int t)
{
	if (l == s && r == t) return Tree[x].Sum;
	push(x, l, r);
	int mid = l + r >> 1;
	if (t <= mid) return Query(ls(x), l, mid, s, t);
	else if (s > mid) return Query(rs(x), mid + 1, r, s, t);
	else return Query(ls(x), l, mid, s, mid) + Query(rs(x), mid + 1, r, mid + 1, t);
}

inline void Solve(int l, int r, int val)
{
	if (l > r) return ;
	if (Tree[1].Min >= r) return ;
	int t = Find(1, 1, n, r);
	if (t < l) return ;
	ans += (1LL * (t - l + 1) * r - Query(1, 1, n, l, t)) * val;
	Modify(1, 1, n, l, t, r);
}

int main()
{
	Prepare();
	scanf("%d", &n);
	for (int i = 1, x; i <= n; i ++)
	{
		scanf("%d", &x);
		for (int j = Head[x]; j; j = h[j].next)
		{
			int d = h[j].node;
			Update(d, i);
		}
	}
	Build(1, 1, n);
	for (int i = M; i; i --)
	{
		if (!Min[i][1]) continue ;
		Solve(Min[i][1] + 1, n, i);
		Solve(Min[i][0] + 1, Max[i][0] - 1, i);
		Solve(1, Max[i][1] - 1, i);
	}
	printf("%I64d\n", ans);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cctype>
#include<vector>
#include<algorithm>
#define ll long long
using namespace std;
int read(){
    char c;int s=0,t=1;
    while(!isdigit(c=getchar()))if(c=='-')t=-1;
    do{s=s*10+c-'0';}while(isdigit(c=getchar()));
    return s*t;
}
const int maxn=200010;
struct tree{int l,r,mins,maxs,delta;ll sum;}t[maxn*4];
int n,a[maxn];
ll h[maxn];
vector<int>v[maxn];
int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a>b?a:b;}
void up(int k){
	t[k].sum=t[k<<1].sum+t[k<<1|1].sum;
	t[k].mins=min(t[k<<1].mins,t[k<<1|1].mins);
	t[k].maxs=max(t[k<<1].maxs,t[k<<1|1].maxs);
}
void modify(int k,int x){t[k].delta=x;t[k].mins=t[k].maxs=x;t[k].sum=1ll*x*(t[k].r-t[k].l+1);}
void down(int k){
	if(t[k].delta){
		modify(k<<1,t[k].delta);
		modify(k<<1|1,t[k].delta);
		t[k].delta=0;
	}
}
void build(int k,int l,int r){
	t[k].l=l;t[k].r=r;t[k].delta=0;
	if(l==r){
		t[k].maxs=t[k].mins=t[k].sum=l;
	}
	else{
		int mid=(l+r)>>1;
		build(k<<1,l,mid);
		build(k<<1|1,mid+1,r);
		up(k);
	}
}
void fix(int k,int l,int r,int x){
	if(l>r||t[k].mins>=x)return;
	if(l<=t[k].l&&t[k].r<=r&&t[k].maxs<=x){modify(k,x);return;}
	down(k);
	int mid=(t[k].l+t[k].r)>>1;
	if(l<=mid)fix(k<<1,l,r,x);
	if(r>mid)fix(k<<1|1,l,r,x);
	up(k);
}
int main(){
	n=read();
	int mx=0;
	for(int i=1;i<=n;i++){
		a[i]=read();
		for(int j=1;j*j<=a[i];j++)if(a[i]%j==0){
			v[j].push_back(i);
			if(j*j!=a[i])v[a[i]/j].push_back(i);
		}
		mx=max(mx,a[i]);
	}
	build(1,1,n);
	for(int i=mx+1;i>=1;i--){
		if(v[i].size()>=2){
			fix(1,v[i][1]+1,n,n+1);
			fix(1,v[i][0]+1,v[i][1],v[i][v[i].size()-1]);
			fix(1,1,v[i][0],v[i][v[i].size()-2]);
		}
		h[i]=1ll*n*(n+1)-t[1].sum;
	}
	ll ans=0;
	//for(int i=1;i<=mx;i++)printf("%lld ",h[i]);puts("");
	for(int i=1;i<=mx;i++)ans+=1ll*(i-1)*(h[i]-h[i-1]);
	printf("%lld",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

const int N=200005;
int n;
int a[N];
long long Ans,ans[N];
int l[N<<2],r[N<<2],tag[N<<2];
long long sum[N<<2];
int l1[N],l2[N],r1[N],r2[N];

inline int read()
{
	int a=0,f=1; char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();}
	while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();}
	return a*f;
}

inline void pushdown(int k)
{
	if (!tag[k]) return;
	tag[k<<1]=tag[k<<1|1]=tag[k];
	sum[k<<1]=(r[k<<1]-l[k<<1]+1)*tag[k];
	sum[k<<1|1]=(r[k<<1|1]-l[k<<1|1]+1)*tag[k];
}

void build(int k,int x,int y)
{
	l[k]=x; r[k]=y;
	if (l[k]==r[k])
	{
		sum[k]=l[k];
		return;
	}
	int mid=l[k]+r[k]>>1;
	build(k<<1,x,mid); build(k<<1|1,mid+1,y);
	sum[k]=sum[k<<1]+sum[k<<1|1];
}

void modify(int k,int x,int y,int val)
{
	if (x>y) return;
	if (l[k]==x&&r[k]==y)
	{
		tag[k]=val;
		sum[k]=(r[k]-l[k]+1)*val;
		return;
	}
	pushdown(k);
	int mid=l[k]+r[k]>>1;
	if (y<=mid) modify(k<<1,x,y,val);
	else if (x>mid) modify(k<<1|1,x,y,val);
	else modify(k<<1,x,mid,val),modify(k<<1|1,mid+1,y,val);
	sum[k]=sum[k<<1]+sum[k<<1|1];
}

int main()
{
	n=read();
	for (int i=1;i<=n;i++) a[i]=read();
	for (int i=1;i<=n;i++)
		for (int j=1,mx=sqrt(a[i]);j<=mx;j++)
			if (a[i]%j==0)
			{
				if (!l1[j]) l1[j]=i;
				else if (!l2[j]) l2[j]=i;
				r2[j]=r1[j]; r1[j]=i;
				if (j*j!=a[i])
				{
					int k=a[i]/j;
					if (!l1[k]) l1[k]=i;
					else if (!l2[k]) l2[k]=i;
					r2[k]=r1[k]; r1[k]=i;
				}
			}
	build(1,1,n);
	for (int i=2e5+1;i;i--)
	{
		if (l1[i]!=r1[i])
		{
			modify(1,1,l1[i],r2[i]);
			modify(1,l1[i]+1,l2[i],r1[i]);
			modify(1,l2[i]+1,n,n+1);
		}
		ans[i]=(long long)n*(n+1)-sum[1];
		if (i<=2e5) Ans+=(ans[i+1]-ans[i])*i;
	}
	cout << Ans << endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

const int N=200005;
int n;
int a[N];
long long Ans,ans[N];
int l[N<<2],r[N<<2],mn[N<<2],mx[N<<2],tag[N<<2];
long long sum[N<<2];
int l1[N],l2[N],r1[N],r2[N];

inline int read()
{
	int a=0,f=1; char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();}
	while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();}
	return a*f;
}

inline void pushup(int k)
{
	sum[k]=sum[k<<1]+sum[k<<1|1];
	mn[k]=min(mn[k<<1],mn[k<<1|1]);
	mx[k]=max(mx[k<<1],mx[k<<1|1]);
}

inline void pushdown(int k)
{
	if (!tag[k]) return;
	mn[k<<1]=mx[k<<1]=mn[k<<1|1]=mx[k<<1|1]=tag[k];
	tag[k<<1]=tag[k<<1|1]=tag[k];
	sum[k<<1]=(r[k<<1]-l[k<<1]+1)*tag[k];
	sum[k<<1|1]=(r[k<<1|1]-l[k<<1|1]+1)*tag[k];
	tag[k]=0;
}

void build(int k,int x,int y)
{
	l[k]=x; r[k]=y;
	if (l[k]==r[k])
	{
		mn[k]=mx[k]=sum[k]=l[k];
		return;
	}
	int mid=l[k]+r[k]>>1;
	build(k<<1,x,mid); build(k<<1|1,mid+1,y);
	pushup(k);
}

void modify(int k,int x,int y,int val)
{
	if (x>y) return;
	if (mn[k]>=val) return;
	if (l[k]==x&&r[k]==y&&mx[k]<=val)
	{
		mn[k]=mx[k]=tag[k]=val;
		sum[k]=(r[k]-l[k]+1)*val;
		return;
	}
	pushdown(k);
	int mid=l[k]+r[k]>>1;
	if (y<=mid) modify(k<<1,x,y,val);
	else if (x>mid) modify(k<<1|1,x,y,val);
	else modify(k<<1,x,mid,val),modify(k<<1|1,mid+1,y,val);
	pushup(k);
}

int main()
{
	n=read();
	for (int i=1;i<=n;i++) a[i]=read();
	for (int i=1;i<=n;i++)
		for (int j=1,mx=sqrt(a[i]);j<=mx;j++)
			if (a[i]%j==0)
			{
				if (!l1[j]) l1[j]=i;
				else if (!l2[j]) l2[j]=i;
				r2[j]=r1[j]; r1[j]=i;
				if (j*j!=a[i])
				{
					int k=a[i]/j;
					if (!l1[k]) l1[k]=i;
					else if (!l2[k]) l2[k]=i;
					r2[k]=r1[k]; r1[k]=i;
				}
			}
	build(1,1,n);
	for (int i=2e5+1;i;i--)
	{
		if (l1[i]!=r1[i])
		{
			modify(1,1,l1[i],r2[i]);
			modify(1,l1[i]+1,l2[i],r1[i]);
			modify(1,l2[i]+1,n,n+1);
		}
		ans[i]=(long long)n*(n+1)-sum[1];
		if (i<=2e5) Ans+=(ans[i+1]-ans[i])*i;
	}
	cout << Ans << endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

const int N=200005;
int n;
int a[N];
long long Ans,ans[N];
int l[N<<2],r[N<<2],mn[N<<2],mx[N<<2],tag[N<<2];
long long sum[N<<2];
int l1[N],l2[N],r1[N],r2[N];

inline int read()
{
	int a=0,f=1; char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();}
	while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();}
	return a*f;
}

inline void pushup(int k)
{
	sum[k]=sum[k<<1]+sum[k<<1|1];
	mn[k]=min(mn[k<<1],mn[k<<1|1]);
	mx[k]=max(mx[k<<1],mx[k<<1|1]);
}

inline void pushdown(int k)
{
	if (!tag[k]) return;
	mn[k<<1]=mx[k<<1]=mn[k<<1|1]=mx[k<<1|1]=tag[k];
	tag[k<<1]=tag[k<<1|1]=tag[k];
	sum[k<<1]=(long long)(r[k<<1]-l[k<<1]+1)*tag[k];
	sum[k<<1|1]=(long long)(r[k<<1|1]-l[k<<1|1]+1)*tag[k];
	tag[k]=0;
}

void build(int k,int x,int y)
{
	l[k]=x; r[k]=y;
	if (l[k]==r[k])
	{
		mn[k]=mx[k]=sum[k]=l[k];
		return;
	}
	int mid=l[k]+r[k]>>1;
	build(k<<1,x,mid); build(k<<1|1,mid+1,y);
	pushup(k);
}

void modify(int k,int x,int y,int val)
{
	if (x>y) return;
	if (mn[k]>=val) return;
	if (l[k]==x&&r[k]==y&&mx[k]<=val)
	{
		mn[k]=mx[k]=tag[k]=val;
		sum[k]=(long long)(r[k]-l[k]+1)*val;
		return;
	}
	pushdown(k);
	int mid=l[k]+r[k]>>1;
	if (y<=mid) modify(k<<1,x,y,val);
	else if (x>mid) modify(k<<1|1,x,y,val);
	else modify(k<<1,x,mid,val),modify(k<<1|1,mid+1,y,val);
	pushup(k);
}

int main()
{
	n=read();
	for (int i=1;i<=n;i++) a[i]=read();
	for (int i=1;i<=n;i++)
		for (int j=1,mx=sqrt(a[i]);j<=mx;j++)
			if (a[i]%j==0)
			{
				if (!l1[j]) l1[j]=i;
				else if (!l2[j]) l2[j]=i;
				r2[j]=r1[j]; r1[j]=i;
				if (j*j!=a[i])
				{
					int k=a[i]/j;
					if (!l1[k]) l1[k]=i;
					else if (!l2[k]) l2[k]=i;
					r2[k]=r1[k]; r1[k]=i;
				}
			}
	build(1,1,n);
	for (int i=2e5+1;i;i--)
	{
		if (l1[i]!=r1[i])
		{
			modify(1,1,l1[i],r2[i]);
			modify(1,l1[i]+1,l2[i],r1[i]);
			modify(1,l2[i]+1,n,n+1);
		}
		ans[i]=(long long)n*(n+1)-sum[1];
		if (i<=2e5) Ans+=(ans[i+1]-ans[i])*i;
	}
	cout << Ans << endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

#ifdef WIN32
	#define LL "%I64d"
#else
	#define LL "%lld"
#endif

#ifdef Lightning
	#define debug(orz...) printf(orz)
	const int DEBUG = 1;
#else
	#define debug(...)
	const int DEBUG = 0;
#endif

#define file_able if (0)
#define setfile(pro_name) freopen(pro_name".in", "r", stdin), freopen(pro_name".out", "w", stdout)
#define closefile() fclose(stdin), fclose(stdout)

#define cmax(_x, _y) (_x < (_y) ? _x = _y : 0)
#define cmin(_x, _y) (_x > (_y) ? _x = _y : 0)
#define gmax(_x, _y) ((_x) > (_y) ? (_x) : (_y))
#define gmin(_x, _y) ((_x) < (_y) ? (_x) : (_y))

namespace defs
{
	#define getc() getchar()

	int read()
	{
		int aa, bb = 1, ch;
		while (ch = getc(), (ch < '0' || ch > '9') && ch != '-');
		ch == '-' ? aa = 0, bb = -1 : aa = ch - '0';
		while (ch = getc(), ch >= '0' && ch <= '9') aa = aa * 10 + ch - '0';
		return aa * bb;
	}

	const unsigned int rand_x = 1401010315;
	const unsigned int rand_y = 1000000019;

	unsigned int rand_cur;

	unsigned int rand_int()
	{
		return rand_cur = rand_cur * rand_x + rand_y;
	}

	const int inf = 1071026353;
	const int mod = 1000000007;
	const int modx = 998244353;

	const int maxn = 200010;
}

namespace st
{
	using namespace defs;

	struct cnode
	{
		int fir, fc, sec;
		long long sum;

		cnode operator +(const cnode &x) const
		{
			int fr = gmin(fir, x.fir);
			int sc = gmin(sec, x.sec);
			int cnt = 0;
			if (fr == fir) cnt += fc;
			else cmin(sc, fir);
			if (fr == x.fir) cnt += x.fc;
			else cmin(sc, x.fir);
			return (cnode) {fr, cnt, sc, sum + x.sum};
		}
	} node[maxn << 2];

	void build(int i, int l, int r)
	{
		node[i] = (cnode) {0, r - l + 1, inf, 0};
		if (l < r)
		{
			int mid = l + r >> 1;
			build(i << 1, l, mid);
			build(i << 1 | 1, mid + 1, r);
		}
	}

	void setlazy(int i, int k)
	{
		if (node[i].sec == 0)
			return;
		if (k <= node[i].fir)
		{
			return;
		}
		if (k < node[i].sec)
		{
			node[i].sum += 1ll * node[i].fc * (k - node[i].fir);
			node[i].fir = k;
		} else
		{
			setlazy(i << 1, k);
			setlazy(i << 1 | 1, k);
			node[i] = node[i << 1] + node[i << 1 | 1];
		}
	}

	void pushdown(int i)
	{
		if (node[i << 1].fir < node[i].fir)
			setlazy(i << 1, node[i].fir);
		if (node[i << 1 | 1].fir < node[i].fir)
			setlazy(i << 1 | 1, node[i].fir);
	}

	void modify(int i, int l, int r, int ml, int mr, int k)
	{
		if (ml > mr) return;
		if (ml <= l && mr >= r)
		{
			setlazy(i, k);
			return;
		}
		pushdown(i);
		int mid = l + r >> 1;
		if (ml <= mid) modify(i << 1, l, mid, ml, mr, k);
		if (mr > mid) modify(i << 1 | 1, mid + 1, r, ml, mr, k);
		node[i] = node[i << 1] + node[i << 1 | 1];
	}

	long long query(int i, int l, int r, int ql, int qr)
	{
		if (ql <= l && qr >= r)
		{
			return node[i].sum;
		}
		pushdown(i);
		int mid = l + r >> 1;
		long long ret = 0;
		if (ql <= mid) ret += query(i << 1, l, mid, ql, qr);
		if (qr > mid) ret += query(i << 1 | 1, mid + 1, r, ql, qr);
		return ret;
	}
}

namespace lightningwork
{
	// my space

	using namespace defs;
	using namespace st;

	int n, num[maxn], fir[maxn], last[maxn];

	void work()
	{
		n = read();
		build(1, 1, n);
		long long sum = 0;
		for (int i = 1; i <= n; ++i)
		{
			num[i] = read();
			int x = num[i], tmp = 0, k;
			for (int j = 1; j * j <= x; ++j)
			{
				if (x % j) continue;
				if (fir[j] == 0) fir[j] = i;
				else cmax(tmp, j);
				if (j < (k = x / j))
				{
					if (fir[k] == 0) fir[k] = i;
					else cmax(tmp, k);
				}
			}
			modify(1, 1, n, i + 1, n, tmp);
		}
		sum = query(1, 1, n, 3, n);
		for (int i = n; i > 1; --i)
		{
			int x = num[i], tmp = 0, k;
			for (int j = 1; j * j <= x; ++j)
			{
				if (x % j) continue;
				if (last[j] == 0)
				{
					last[j] = i;
					if (fir[j] < i) modify(1, 1, n, fir[j] + 1, i - 1, j);
				} else cmax(tmp, j);
				if (j < (k = x / j))
				{
					if (last[k] == 0)
					{
						last[k] = i;
						if (fir[k] < i) modify(1, 1, n, fir[k] + 1, i - 1, k);
					}
					else cmax(tmp, k);
				}
			}
			modify(1, 1, n, 1, i - 1, tmp);
			sum += query(1, 1, n, i == n ? 2 : 1, i - 1);
		}
		cout << sum << endl;
	}
}

int main()
{
	file_able setfile("work");
	lightningwork::work();
	file_able closefile();
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <iostream>
using namespace std;
#define N 200005
#define ll long long
#define ls rt<<1
#define rs rt<<1|1
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
ll sum[N<<2],ans,last;
int minx[N<<2],minn[N<<2],cn[N<<2],n;
vector<int> v[N];
void PushUp(int rt)
{
	sum[rt]=sum[ls]+sum[rs];
	if(minn[ls]<minn[rs])minn[rt]=minn[ls],cn[rt]=cn[ls],minx[rt]=min(minn[rs],minx[ls]);
	else if(minn[ls]=minn[rs])minn[rt]=minn[ls],cn[rt]=cn[ls]+cn[rs],minx[rt]=min(minx[rs],minx[ls]);
	else minn[rt]=minn[rs],cn[rt]=cn[rs],minx[rt]=min(minn[ls],minx[rs]);
}
void PushDown(int rt)
{
	if(minn[ls]<minn[rt])
	{
		sum[ls]+=1ll*(minn[rt]-minn[ls])*cn[ls],minn[ls]=minn[rt];
	}
	if(minn[rs]<minn[rt])
	{
		sum[rs]+=1ll*(minn[rt]-minn[ls])*cn[rs],minn[rs]=minn[rt];
	}
}
void build(int l,int r,int rt)
{
	if(l==r)
	{
		minn[rt]=l;sum[rt]=l;cn[rt]=1;minx[rt]=1<<30;
		return ;
	}
	int m=(l+r)>>1;
	build(lson);build(rson);
	PushUp(rt);
}
void Update(int L,int R,int c,int l,int r,int rt)
{
	if(minn[rt]>=c)return ;
	if(L<=l&&r<=R&&minx[rt]>c)
	{
		sum[rt]+=1ll*(c-minn[rt])*cn[rt],minn[rt]=c;
		return ;
	}
	PushDown(rt);int m=(l+r)>>1;
	if(L<=m)Update(L,R,c,lson);if(R>m)Update(L,R,c,rson);PushUp(rt);
}
void get(int x,int y)
{
	for(int i=1;i*i<=x;i++)
	{
		if(!(x%i))
		{
			v[i].push_back(y);
			v[x/i].push_back(y);
		}
	}
}
int main()
{
	scanf("%d",&n);
	int o=0;
	for(int i=1;i<=n;i++)
	{
		int x;scanf("%d",&x);
		get(x,i);o=max(o,x);
	}
	build(1,n,1);last=1ll*n*(n+1)-sum[1];
	for(int i=o;i>=1;i--)
	{
		int x=v[i].size();
		//printf("%lld\n",ans);
		if(x<=1)continue;
		else if(x>=2)
		{
			//printf("%d %d\n",v[i][0],v[i][1]);
			Update(1,v[i][0],v[i][x-2],1,n,1);
			Update(v[i][0]+1,v[i][1],v[i][x-1],1,n,1);
			if(v[i][1]!=n)Update(v[i][1]+1,n,n+1,1,n,1);
			ll t=1ll*n*(n+1)-sum[1];
			ans+=1ll*(last-t)*i;
			last=t;
		}
	}
	printf("%lld\n",ans);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <iostream>
using namespace std;
#define N 200005
#define ll long long
#define ls rt<<1
#define rs rt<<1|1
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
ll sum[N<<2],ans,last;
int minx[N<<2],minn[N<<2],cn[N<<2],n;
vector<int> v[N];
void PushUp(int rt)
{
	sum[rt]=sum[ls]+sum[rs];
	if(minn[ls]<minn[rs])minn[rt]=minn[ls],cn[rt]=cn[ls],minx[rt]=min(minn[rs],minx[ls]);
	else if(minn[ls]=minn[rs])minn[rt]=minn[ls],cn[rt]=cn[ls]+cn[rs],minx[rt]=min(minx[rs],minx[ls]);
	else minn[rt]=minn[rs],cn[rt]=cn[rs],minx[rt]=min(minn[ls],minx[rs]);
}
void PushDown(int rt)
{
	if(minn[ls]<minn[rt])
	{
		sum[ls]+=1ll*(minn[rt]-minn[ls])*cn[ls],minn[ls]=minn[rt];
	}
	if(minn[rs]<minn[rt])
	{
		sum[rs]+=1ll*(minn[rt]-minn[ls])*cn[rs],minn[rs]=minn[rt];
	}
}
void build(int l,int r,int rt)
{
	if(l==r)
	{
		minn[rt]=l;sum[rt]=l;cn[rt]=1;minx[rt]=1<<30;
		return ;
	}
	int m=(l+r)>>1;
	build(lson);build(rson);
	PushUp(rt);
}
void Update(int L,int R,int c,int l,int r,int rt)
{
	if(minn[rt]>=c)return ;
	if(L<=l&&r<=R&&minx[rt]>c)
	{
		sum[rt]+=1ll*(c-minn[rt])*cn[rt],minn[rt]=c;
		return ;
	}
	PushDown(rt);int m=(l+r)>>1;
	if(L<=m)Update(L,R,c,lson);if(R>m)Update(L,R,c,rson);PushUp(rt);
}
void get(int x,int y)
{
	for(int i=1;i*i<=x;i++)
	{
		if(!(x%i))
		{
			v[i].push_back(y);
			if(i*i==x)continue;
			v[x/i].push_back(y);
		}
	}
}
int main()
{
	scanf("%d",&n);
	int o=0;
	for(int i=1;i<=n;i++)
	{
		int x;scanf("%d",&x);
		get(x,i);o=max(o,x);
	}
	build(1,n,1);last=1ll*n*(n+1)-sum[1];
	for(int i=o;i>=1;i--)
	{
		int x=v[i].size();
		//printf("%lld\n",ans);
		if(x<=1)continue;
		else if(x>=2)
		{
			//printf("%d %d\n",v[i][0],v[i][1]);
			Update(1,v[i][0],v[i][x-2],1,n,1);
			Update(v[i][0]+1,v[i][1],v[i][x-1],1,n,1);
			if(v[i][1]!=n)Update(v[i][1]+1,n,n+1,1,n,1);
			ll t=1ll*n*(n+1)-sum[1];
			ans+=1ll*(last-t)*i;
			last=t;
		}
	}
	printf("%lld\n",ans);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <iostream>
using namespace std;
#define N 200005
#define ll long long
#define ls rt<<1
#define rs rt<<1|1
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
ll sum[N<<2],ans,last;
int minx[N<<2],minn[N<<2],cn[N<<2],n;
vector<int> v[N];
void PushUp(int rt)
{
	sum[rt]=sum[ls]+sum[rs];
	if(minn[ls]<minn[rs])minn[rt]=minn[ls],cn[rt]=cn[ls],minx[rt]=min(minn[rs],minx[ls]);
	else if(minn[ls]==minn[rs])minn[rt]=minn[ls],cn[rt]=cn[ls]+cn[rs],minx[rt]=min(minx[rs],minx[ls]);
	else minn[rt]=minn[rs],cn[rt]=cn[rs],minx[rt]=min(minn[ls],minx[rs]);
}
void PushDown(int rt)
{
	if(minn[ls]<minn[rt])
	{
		sum[ls]+=1ll*(minn[rt]-minn[ls])*cn[ls],minn[ls]=minn[rt];
	}
	if(minn[rs]<minn[rt])
	{
		sum[rs]+=1ll*(minn[rt]-minn[rs])*cn[rs],minn[rs]=minn[rt];
	}
}
void build(int l,int r,int rt)
{
	if(l==r)
	{
		minn[rt]=sum[rt]=l;cn[rt]=1;minx[rt]=1<<30;
		return ;
	}
	int m=(l+r)>>1;
	build(lson);build(rson);
	PushUp(rt);
}
void Update(int L,int R,int c,int l,int r,int rt)
{
	//printf("%d %d %d %d %d %d\n",l,r,L,R,c,minn[rt]);
	if(minn[rt]>=c)return ;
	if(L<=l&&r<=R&&minx[rt]>c)
	{
		sum[rt]+=1ll*(c-minn[rt])*cn[rt],minn[rt]=c;
		return ;
	}
	PushDown(rt);int m=(l+r)>>1;
	if(L<=m)Update(L,R,c,lson);if(R>m)Update(L,R,c,rson);PushUp(rt);
}
void get(int x,int y)
{
	for(int i=1;i*i<=x;i++)
	{
		if(!(x%i))
		{
			v[i].push_back(y);
			if(i*i==x)continue;
			v[x/i].push_back(y);
		}
	}
}
int query(int x,int l,int r,int rt)
{
	if(l==r)return minn[rt];
	PushDown(rt);int m=(l+r)>>1;
	if(x<=m)return query(x,lson);return query(x,rson);
}
int main()
{
	scanf("%d",&n);
	int o=0;
	for(int i=1;i<=n;i++)
	{
		int x;scanf("%d",&x);
		get(x,i);o=max(o,x);
	}
	build(1,n,1);last=1ll*n*(n+1)-sum[1];
	for(int i=o;i>=1;i--)
	{
		int x=v[i].size();
		//if(i==1)printf("%d\n",last);
		if(x>=2)
		{
			//printf("%d %d\n",v[i][x-2],v[i][x-1]);
			Update(1,v[i][0],v[i][x-2],1,n,1);
			Update(v[i][0]+1,v[i][1],v[i][x-1],1,n,1);
			if(v[i][1]!=n)Update(v[i][1]+1,n,n+1,1,n,1);
			//printf("%d\n",sum[1]);
			ll t=1ll*n*(n+1)-sum[1];
			ans+=1ll*(last-t)*i;
			last=t;
		//printf("%lld\n",ans);
		}
		//if(i==1)
		//{
			// int ret=0;
			// for(int j=1;j<=n;j++)ret=ret+query(j,1,n,1);
			// printf("%d\n",ret);
		// }
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll N=200200;
ll i,j,k,n,ma,ch,ans,nm;
ll v[N],H[N];
struct tree {ll sum,same,lx;} T[N<<2];
vector <ll> A[N];
void R(ll &x)
{
	x=0;ch=getchar();
	while (ch<'0' || '9'<ch) ch=getchar();
	while ('0'<=ch && ch<='9') x=x*10+ch-'0',ch=getchar();
}
void W(ll x)
{
	if (x>=10) W(x/10);
	putchar(x%10+'0');
}
void down(ll L,ll R,ll k)
{
	if (T[k].same!=-1)
	{
		ll mid=(L+R)>>1;
		T[k<<1].same=T[k<<1|1].same=T[k].same;
		T[k<<1].sum=T[k].same*(mid-L+1);
		T[k<<1|1].sum=T[k].same*(R-mid);
		T[k].same=-1;
	}
}
void up(ll k)
{
	T[k].sum=T[k<<1].sum+T[k<<1|1].sum;
	T[k].lx=T[k<<1].lx;
}
void T_build(ll L,ll R,ll k)
{
	T[k].same=-1;
	if (L==R)
	{
		T[k].lx=T[k].sum=L;
		return;
	}
	ll mid=(L+R)>>1;
	T_build(L,mid,k<<1);
	T_build(mid+1,R,k<<1|1);
	up(k);
}
void T_same(ll L,ll R,ll l,ll r,ll x,ll k)
{
	if (L==l && R==r)
	{
		T[k].lx=T[k].same=x;
		T[k].sum=T[k].same*(R-L+1);
		return;
	}
	down(L,R,k);
	ll mid=(L+R)>>1;
	if (r<=mid) T_same(L,mid,l,r,x,k<<1);
	else
	{
		if (l>mid) T_same(mid+1,R,l,r,x,k<<1|1);
		else T_same(L,mid,l,mid,x,k<<1),T_same(mid+1,R,mid+1,r,x,k<<1|1);
	}
	up(k);
}
ll T_lower(ll L,ll R,ll x,ll k)
{
	if (L==R) return L;
	ll mid=(L+R)>>1;
	if (T[k<<1|1].lx>=x) return T_lower(L,mid,x,k<<1);
	return T_lower(mid+1,R,x,k<<1|1);
}
void Same(ll l,ll r,ll x)
{
	if (T[1].lx>=x) return;
	ll t=T_lower(1,n,x,1);
	if (t<r) r=t;
	if (l<=r) T_same(1,n,l,r,x,1);
}
int main()
{
	R(n);nm=n*(n+1);
	for (i=1;i<=n;i++)
	{
		R(k);
		v[k]=i;
		if (k>ma) ma=k;
	}
	for (i=1;i<=ma;i++)
		for (j=i;j<=ma;j+=i) if (v[j]) A[i].push_back(v[j]);
	T_build(1,n,1);
	H[ma]=nm-T[1].sum;
	for (i=ma;i;i--)
	{
		if (A[i].size()>1)
		{
			sort(A[i].begin(),A[i].end());
			k=A[i].size();
			Same(A[i][1]+1,n,n+1);
			Same(A[i][0]+1,A[i][1],A[i][k-1]);
			Same(1,A[i][0],A[i][k-2]);
		}
		H[i-1]=nm-T[1].sum;
	}
	for (i=1;i<=ma;i++) ans+=i*(H[i]-H[i-1]);
	W(ans);puts("");
}


#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll N=200200;
ll i,j,k,n,ma,ch,ans,nm;
ll v[N],H[N];
struct tree {ll sum,same,lx;} T[N<<2];
vector <ll> A[N];
void R(ll &x)
{
	x=0;ch=getchar();
	while (ch<'0' || '9'<ch) ch=getchar();
	while ('0'<=ch && ch<='9') x=x*10+ch-'0',ch=getchar();
}
void W(ll x)
{
	if (x>=10) W(x/10);
	putchar(x%10+'0');
}
void down(ll L,ll R,ll k)
{
	if (T[k].same!=-1)
	{
		ll mid=(L+R)>>1;
		T[k<<1].lx=T[k<<1|1].lx=T[k].same;
		T[k<<1].same=T[k<<1|1].same=T[k].same;
		T[k<<1].sum=T[k].same*(mid-L+1);
		T[k<<1|1].sum=T[k].same*(R-mid);
		T[k].same=-1;
	}
}
void up(ll k)
{
	T[k].sum=T[k<<1].sum+T[k<<1|1].sum;
	T[k].lx=T[k<<1].lx;
}
void T_build(ll L,ll R,ll k)
{
	T[k].same=-1;
	if (L==R)
	{
		T[k].lx=T[k].sum=L;
		return;
	}
	ll mid=(L+R)>>1;
	T_build(L,mid,k<<1);
	T_build(mid+1,R,k<<1|1);
	up(k);
}
void T_same(ll L,ll R,ll l,ll r,ll x,ll k)
{
	if (L==l && R==r)
	{
		T[k].lx=T[k].same=x;
		T[k].sum=T[k].same*(R-L+1);
		return;
	}
	down(L,R,k);
	ll mid=(L+R)>>1;
	if (r<=mid) T_same(L,mid,l,r,x,k<<1);
	else
	{
		if (l>mid) T_same(mid+1,R,l,r,x,k<<1|1);
		else T_same(L,mid,l,mid,x,k<<1),T_same(mid+1,R,mid+1,r,x,k<<1|1);
	}
	up(k);
}
ll T_lower(ll L,ll R,ll x,ll k)
{
	if (L==R) return L;
	down(L,R,k);
	ll mid=(L+R)>>1;
	if (T[k<<1|1].lx>=x) return T_lower(L,mid,x,k<<1);
	return T_lower(mid+1,R,x,k<<1|1);
}
void Same(ll l,ll r,ll x)
{
	if (T[1].lx>=x) return;
	ll t=T_lower(1,n,x,1);
	if (t<r) r=t;
	if (l<=r) T_same(1,n,l,r,x,1);
}
int main()
{
	R(n);nm=n*(n+1);
	for (i=1;i<=n;i++)
	{
		R(k);
		v[k]=i;
		if (k>ma) ma=k;
	}
	for (i=1;i<=ma;i++)
		for (j=i;j<=ma;j+=i) if (v[j]) A[i].push_back(v[j]);
	T_build(1,n,1);
	H[ma]=nm-T[1].sum;
	for (i=ma;i;i--)
	{
		if (A[i].size()>1)
		{
			sort(A[i].begin(),A[i].end());
			k=A[i].size();
			Same(A[i][1]+1,n,n+1);
			Same(A[i][0]+1,A[i][1],A[i][k-1]);
			Same(1,A[i][0],A[i][k-2]);
		}
		H[i-1]=nm-T[1].sum;
	}
	for (i=1;i<=ma;i++) ans+=i*(H[i]-H[i-1]);
	W(ans);puts("");
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define ppi pair<pii,int>
#define L first.first
#define R first.second
#define T second
#define pb push_back
#define maxn 200200
#define pit set<ppi> :: iterator
#define rit set<ppi> :: reverse_iterator
#define ll long long
#define debug
using namespace std;

vector<int> pos[maxn];
vector<pii> sw[maxn];

ll sum = 0;
set<ppi> S;

pit rem(pit it){
    debug("- %d~%d %d\n",it->L,it->R,it->T);
    pit it2 = it;
    it2++;
    sum -= (ll) (it->R-it->L+1) * (it->T);
    S.erase(it);
    return it2;
}

void add(int l,int r,int s){
    if(l > r) return;
    debug("+ %d~%d %d\n",l,r,s);
    sum += (ll) (r-l+1) * s;
    S.insert(ppi(pii(l,r),s));
}

int main() {
    int n;
    ll ans = 0;
    scanf("%d",&n);
    for(int i=1,a;i<=n;i++){
        scanf("%d",&a);
        pos[a].pb(i);
    }
    for(int i=1;i<maxn;i++){
        vector<int> v;
        for(int j=i;j<maxn;j+=i)
            for(int k=0;k<pos[j].size();k++)
                v.pb(pos[j][k]);
        sort(v.begin(),v.end());
        if(v.size() < 2) continue;
        sw[n+1].pb(pii(v[1],i));
        sw[v[v.size()-1]].pb(pii(v[0],i));
        sw[v[v.size()-2]].pb(pii(0,i));
    }

    add(0,n+1,0);
    for(int i=n+1;i>=2;i--){
        for(int j=0;j<sw[i].size();j++){
            int x = sw[i][j].first, y = sw[i][j].first-1, val = sw[i][j].second;
            debug("x %d val%d\n",x,val);
            pit it = S.lower_bound(ppi(pii(x+1,-1),-1));
            it--;
            while(it != S.end()){
                if(it->T >= val)
                    break;
                y = it->R;
                if(it->L < x){
                    int a = it->L, b = x-1, c = it->T;
                    it = rem(it);
                    add(a,b,c);
                }
                else
                    it = rem(it);
            }
            add(x,y,val);
        }
        while(S.size() && S.rbegin()->R >= i-1){
            if(S.rbegin()->L >= i-1)
                rem(--S.end());
            else {
                int a = S.rbegin()->L, b = i-2, c = S.rbegin()->T;
                rem(--S.end());
                add(a,b,c);
            }
        }
        ans += sum;
        debug("+ %lld\n",sum);
    }
    printf("%lld\n",ans);
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define ppi pair<pii,int>
#define L first.first
#define R first.second
#define T second
#define pb push_back
#define maxn 200200
#define pit set<ppi> :: iterator
#define rit set<ppi> :: reverse_iterator
#define ll long long
#define debug
using namespace std;

vector<int> pos[maxn];
vector<pii> sw[maxn];

ll sum = 0;
set<ppi> S;

pit rem(pit it){
    debug("- %d~%d %d\n",it->L,it->R,it->T);
    pit it2 = it;
    it2++;
    sum -= (ll) (it->R-it->L+1) * (it->T);
    S.erase(it);
    return it2;
}

void add(int l,int r,int s){
    debug("+ %d~%d %d\n",l,r,s);
    sum += (ll) (r-l+1) * s;
    S.insert(ppi(pii(l,r),s));
}

int main() {
    int n;
    ll ans = 0;
    scanf("%d",&n);
    for(int i=1,a;i<=n;i++){
        scanf("%d",&a);
        pos[a].pb(i);
    }
    for(int i=1;i<maxn;i++){
        vector<int> v;
        for(int j=i;j<maxn;j+=i)
            for(int k=0;k<pos[j].size();k++)
                v.pb(pos[j][k]);
        sort(v.begin(),v.end());
        if(v.size() < 2) continue;
        sw[n+1].pb(pii(v[1],i));
        sw[v[v.size()-1]].pb(pii(v[0],i));
        sw[v[v.size()-2]].pb(pii(0,i));
    }

    add(0,n+1,0);
    for(int i=n+1;i>=0;i--){
        for(int j=0;j<sw[i].size();j++){
            int x = sw[i][j].first, y = sw[i][j].first-1, val = sw[i][j].second;
            debug("x %d val%d\n",x,val);
            pit it = S.lower_bound(ppi(pii(x+1,-1),-1));
            it--;
            while(it != S.end()){
                if(it->T >= val)
                    break;
                y = it->R;
                if(it->L < x){
                    int a = it->L, b = x-1, c = it->T;
                    it = rem(it);
                    add(a,b,c);
                }
                else
                    it = rem(it);
            }
            add(x,y,val);
        }
        while(S.size() && S.rbegin()->R >= i-1){
            if(S.rbegin()->L >= i-1)
                rem(--S.end());
            else {
                int a = S.rbegin()->L, b = i-2, c = S.rbegin()->T;
                rem(--S.end());
                add(a,b,c);
            }
        }
        ans += sum;
    }
    printf("%lld\n",ans);
}


#include <algorithm>
#include <cstdio>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
typedef long long ll;
vector<int> mul[200010];
struct Node {
  int cover, min;
  ll sum;
}seg[200010*3];
void build(int L, int R, int k) {
  if (L == R) {
    seg[k].sum = seg[k].min = L;
    seg[k].cover = 0;
    return;
  }
  build(L, (L + R) / 2, 2 * k);
  build((L + R) / 2 + 1, R, 2 * k + 1);
  seg[k].sum = seg[2*k].sum + seg[2*k+1].sum;
  seg[k].min = min(seg[2*k].min, seg[2*k+1].min);
  seg[k].cover = 0;
}
void down(int k, int L, int R) {
  seg[2*k].cover = seg[k].cover;
  seg[2*k].min = seg[k].cover;
  seg[2*k].sum = (ll)seg[k].cover*((L+R)/2-L+1);
  seg[2*k+1].cover = seg[k].cover;
  seg[2*k+1].min = seg[k].cover;
  seg[2*k+1].sum = (ll)seg[k].cover*(R-(L+R)/2);
  seg[k].cover = 0;
}

ll cal_sum(int l, int r, int L, int R, int k) {
  if (L >= l && R <= r) {
    return seg[k].sum;
  }
  if (R < l || L > r) {
    return 0;
  }
  if (seg[k].cover) {
    down(k, L, R);
  }
  ll s1 = cal_sum(l, r, L, (L + R) / 2, 2 * k);
  ll s2 = cal_sum(l, r, (L + R + 1) / 2, R, 2 * k + 1);
  return s1 + s2;
}
void update(int l, int r, int L, int R, int k, int v) {
  if (L >= l && R <= r) {
    seg[k].cover = v;
    seg[k].min = v;
    seg[k].sum = (ll)v*(R-L+1);
    return;
  }
  if (R < l || L > r) {
    return;
  }
  if (seg[k].cover) {
    down(k, L, R);
  }
  update(l, r, L, (L+R)/2, 2*k, v);
  update(l, r, (L+R)/2+1, R, 2*k+1, v);
  seg[k].sum = seg[2*k].sum + seg[2*k+1].sum;
  seg[k].min = min(seg[2*k].min, seg[2*k+1].min);
}
int query(int l, int r, int L, int R, int k, int v) {
  if (R < l || L > r) {
    return -1;
  }
  if (seg[k].min > v) {
    return -1;
  }
  if (L == R) {
    return L;
  }
  if (seg[k].cover) {
    down(k, L, R);
  }
  int mid = (L+R)/2;
  if (mid+1 <= r && seg[2*k+1].min <= v) {
    return query(l, r, (L+R)/2+1, R, 2*k+1, v);
  }
  return query(l, r, L, (L+R)/2, 2*k, v);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int temp;
    scanf("%d", &temp);
    for (int j = 1; j * j <= temp; j++) {
      if (temp % j == 0) {
        mul[j].push_back(i);
        if (j * j != temp) {
          mul[temp/j].push_back(i);
        }
      }
    }
  }
  build(1, n, 1);
  //cout << cal_sum(1, 2, 1, n, 1) << endl;
  //cout << query(1, n, 1, 1) << endl;
  ll ans = 0, pre = cal_sum(1, n, 1, n, 1);
  for (int i = n; i >= 1; i--) {
    //for (size_t j = 0; j < mul[i].size(); j++) {
      //cout << mul[i][j] << " ";
    //}
    //cout << endl;
    //for (int j = 1; j <= 5; j++) {
      //cout << seg[j].cover << " " << seg[j].min << " " << seg[j].sum << endl;
    //}
    if (mul[i].size() >= 2) {
      int k = mul[i].size();
      update(mul[i][1]+1, n, 1, n, 1, n+1);
      int pos = query(mul[i][0]+1, mul[i][1], 1, n, 1, mul[i][k-1]);
      //cout << "pos1: " << pos << endl;
      if (pos != -1) {
        update(mul[i][0]+1, pos, 1, n, 1, mul[i][k-1]);
      }
      pos = query(1, mul[i][0], 1, n, 1, mul[i][k-2]);
      //cout << "pos2: " << pos << endl;
      if (pos != -1) {
        update(1, pos, 1, n, 1, mul[i][k-2]);
      }
    }
    ll temp = cal_sum(1, n, 1, n, 1);
    //cout << "pre: " << pre << " temp: " << temp << endl;
    ans += (temp - pre) * i;
    pre = temp;
  }
  cout << ans << endl;
}


#include <algorithm>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
typedef long long ll;
vector<int> mul[200010];
struct Node {
  int cover, min;
  ll sum;
}seg[200010*3];
void build(int L, int R, int k) {
  if (L == R) {
    seg[k].sum = seg[k].min = L;
    seg[k].cover = 0;
    return;
  }
  build(L, (L + R) / 2, 2 * k);
  build((L + R) / 2 + 1, R, 2 * k + 1);
  seg[k].sum = seg[2*k].sum + seg[2*k+1].sum;
  seg[k].min = min(seg[2*k].min, seg[2*k+1].min);
  seg[k].cover = 0;
}
void down(int k, int L, int R) {
  seg[2*k].cover = seg[k].cover;
  seg[2*k].min = seg[k].cover;
  seg[2*k].sum = (ll)seg[k].cover*((L+R)/2-L+1);
  seg[2*k+1].cover = seg[k].cover;
  seg[2*k+1].min = seg[k].cover;
  seg[2*k+1].sum = (ll)seg[k].cover*(R-(L+R)/2);
  seg[k].cover = 0;
}

ll cal_sum(int l, int r, int L, int R, int k) {
  if (L >= l && R <= r) {
    return seg[k].sum;
  }
  if (R < l || L > r) {
    return 0;
  }
  if (seg[k].cover) {
    down(k, L, R);
  }
  ll s1 = cal_sum(l, r, L, (L + R) / 2, 2 * k);
  ll s2 = cal_sum(l, r, (L + R + 1) / 2, R, 2 * k + 1);
  return s1 + s2;
}
void update(int l, int r, int L, int R, int k, int v) {
  if (L >= l && R <= r) {
    seg[k].cover = v;
    seg[k].min = v;
    seg[k].sum = (ll)v*(R-L+1);
    return;
  }
  if (R < l || L > r) {
    return;
  }
  if (seg[k].cover) {
    down(k, L, R);
  }
  update(l, r, L, (L+R)/2, 2*k, v);
  update(l, r, (L+R)/2+1, R, 2*k+1, v);
  seg[k].sum = seg[2*k].sum + seg[2*k+1].sum;
  seg[k].min = min(seg[2*k].min, seg[2*k+1].min);
}
int query(int l, int r, int L, int R, int k, int v) {
  if (R < l || L > r) {
    return -1;
  }
  if (seg[k].min > v) {
    return -1;
  }
  if (L == R) {
    return L;
  }
  if (seg[k].cover) {
    down(k, L, R);
  }
  int mid = (L+R)/2;
  if (mid+1 <= r && seg[2*k+1].min <= v) {
    return query(l, r, (L+R)/2+1, R, 2*k+1, v);
  }
  return query(l, r, L, (L+R)/2, 2*k, v);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int temp;
    scanf("%d", &temp);
    for (int j = 1; j * j <= temp; j++) {
      if (temp % j == 0) {
        mul[j].push_back(i);
        if (j * j != temp) {
          mul[temp/j].push_back(i);
        }
      }
    }
  }
  build(1, n, 1);
  //cout << cal_sum(1, 2, 1, n, 1) << endl;
  //cout << query(1, n, 1, 1) << endl;
  ll ans = 0, pre = cal_sum(1, n, 1, n, 1);
  for (int i = n; i >= 1; i--) {
    //for (size_t j = 0; j < mul[i].size(); j++) {
      //cout << mul[i][j] << " ";
    //}
    //cout << endl;
    //for (int j = 1; j <= 5; j++) {
      //cout << seg[j].cover << " " << seg[j].min << " " << seg[j].sum << endl;
    //}
    if (mul[i].size() >= 2) {
      int k = mul[i].size();
      update(mul[i][1]+1, n, 1, n, 1, n+1);
      int pos = query(mul[i][0]+1, mul[i][1], 1, n, 1, mul[i][k-1]);
      //cout << "pos1: " << pos << endl;
      if (pos != -1) {
        update(mul[i][0]+1, pos, 1, n, 1, mul[i][k-1]);
      }
      pos = query(1, mul[i][0], 1, n, 1, mul[i][k-2]);
      //cout << "pos2: " << pos << endl;
      if (pos != -1) {
        update(1, pos, 1, n, 1, mul[i][k-2]);
      }
    }
    ll temp = cal_sum(1, n, 1, n, 1);
    //cout << "pre: " << pre << " temp: " << temp << endl;
    ans += (temp - pre) * i;
    pre = temp;
  }
  cout << ans << endl;
}


#include <algorithm>
#include <cstdio>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
typedef long long ll;
vector<int> mul[200010];
struct Node {
  int cover, min;
  ll sum;
}seg[200010*3];
void build(int L, int R, int k) {
  if (L == R) {
    seg[k].sum = seg[k].min = L;
    seg[k].cover = 0;
    return;
  }
  build(L, (L + R) / 2, 2 * k);
  build((L + R) / 2 + 1, R, 2 * k + 1);
  seg[k].sum = seg[2*k].sum + seg[2*k+1].sum;
  seg[k].min = min(seg[2*k].min, seg[2*k+1].min);
  seg[k].cover = 0;
}
void down(int k, int L, int R) {
  seg[2*k].cover = seg[k].cover;
  seg[2*k].min = seg[k].cover;
  seg[2*k].sum = (ll)seg[k].cover*((L+R)/2-L+1);
  seg[2*k+1].cover = seg[k].cover;
  seg[2*k+1].min = seg[k].cover;
  seg[2*k+1].sum = (ll)seg[k].cover*(R-(L+R)/2);
  seg[k].cover = 0;
}

ll cal_sum(int l, int r, int L, int R, int k) {
  if (L >= l && R <= r) {
    return seg[k].sum;
  }
  if (R < l || L > r) {
    return 0;
  }
  if (seg[k].cover) {
    down(k, L, R);
  }
  ll s1 = cal_sum(l, r, L, (L + R) / 2, 2 * k);
  ll s2 = cal_sum(l, r, (L + R + 1) / 2, R, 2 * k + 1);
  return s1 + s2;
}
void update(int l, int r, int L, int R, int k, int v) {
  if (L >= l && R <= r) {
    seg[k].cover = v;
    seg[k].min = v;
    seg[k].sum = (ll)v*(R-L+1);
    return;
  }
  if (R < l || L > r) {
    return;
  }
  if (seg[k].cover) {
    down(k, L, R);
  }
  update(l, r, L, (L+R)/2, 2*k, v);
  update(l, r, (L+R)/2+1, R, 2*k+1, v);
  seg[k].sum = seg[2*k].sum + seg[2*k+1].sum;
  seg[k].min = min(seg[2*k].min, seg[2*k+1].min);
}
int query(int l, int r, int L, int R, int k, int v) {
  if (R < l || L > r) {
    return -1;
  }
  if (seg[k].min > v) {
    return -1;
  }
  if (L == R) {
    return L;
  }
  if (seg[k].cover) {
    down(k, L, R);
  }
  int mid = (L+R)/2;
  if (mid+1 <= r && seg[2*k+1].min <= v) {
    return query(l, r, (L+R)/2+1, R, 2*k+1, v);
  }
  return query(l, r, L, (L+R)/2, 2*k, v);
}
int main() {
  int n;
  scanf("%d", &n);
  int mx = -1;
  for (int i = 1; i <= n; i++) {
    int temp;
    scanf("%d", &temp);
    for (int j = 1; j * j <= temp; j++) {
      if (temp % j == 0) {
        mul[j].push_back(i);
        if (j * j != temp) {
          mul[temp/j].push_back(i);
        }
      }
    }
    if (temp > mx) {
      mx = temp;
    }
  }
  build(1, n, 1);
  //cout << cal_sum(1, 2, 1, n, 1) << endl;
  //cout << query(1, n, 1, 1) << endl;
  ll ans = 0, pre = cal_sum(1, n, 1, n, 1);
  for (int i = mx; i >= 1; i--) {
    //for (size_t j = 0; j < mul[i].size(); j++) {
      //cout << mul[i][j] << " ";
    //}
    //cout << endl;
    //for (int j = 1; j <= 5; j++) {
      //cout << seg[j].cover << " " << seg[j].min << " " << seg[j].sum << endl;
    //}
    if (mul[i].size() >= 2) {
      int k = mul[i].size();
      update(mul[i][1]+1, n, 1, n, 1, n+1);
      int pos = query(mul[i][0]+1, mul[i][1], 1, n, 1, mul[i][k-1]);
      //cout << "pos1: " << pos << endl;
      if (pos != -1) {
        update(mul[i][0]+1, pos, 1, n, 1, mul[i][k-1]);
      }
      pos = query(1, mul[i][0], 1, n, 1, mul[i][k-2]);
      //cout << "pos2: " << pos << endl;
      if (pos != -1) {
        update(1, pos, 1, n, 1, mul[i][k-2]);
      }
    }
    ll temp = cal_sum(1, n, 1, n, 1);
    //cout << "pre: " << pre << " temp: " << temp << endl;
    ans += (temp - pre) * i;
    pre = temp;
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>
#define LL long long
#define ls rt << 1
#define rs rt << 1 | 1
#define lson ls, l, m
#define rson rs, m + 1, r
#define pii pair <int, int>
#define xx first
#define yy second
using namespace std;

const int N = 200100, INF = 1e9;
int l[2][N], r[2][N];
set <pii> st;
set <pii> :: iterator it;

void updata (int x, int p) {
    if (!l[0][x] || l[0][x] > p) {
        l[1][x] = l[0][x];
        l[0][x] = p;
    } else if (!l[1][x] || l[1][x] > p) l[1][x] = p;
    if (!r[0][x] || r[0][x] < p) {
        r[1][x] = r[0][x];
        r[0][x] = p;
    } else if (!r[1][x] || r[1][x] < p) r[1][x] = p;
}

LL area;

void solve (int x, int p) {
    if (x > p) return;
//    cout << x << ' ' << p << endl;
    it = st.lower_bound (pii (x, INF));
    it--;
    if ((*it).yy >= p) return;
    int nx = x, np = (*it).yy;
//    cout << nx << ' ' << np << endl;
//    cout << (*it).xx << ' ' << (*it).yy << endl;
    if ((*it).xx != x) it++;
    while (it != st.end()) {
//        cout << nx << ' ' << np << ' ' << (*it).xx << ' ' << (*it).yy << endl;
        area += 1LL * ((*it).xx - nx) * (p - np);
        nx = (*it).xx;
        np = (*it).yy;
        if (np > p) break;
        st.erase(it++);
    }
    st.insert(pii(x, p));
}

int main () {
//    freopen ("in.txt", "r", stdin);
    int n, a;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        scanf ("%d", &a);
        for (int j = 1; j * j <= a; j++) if (a % j == 0) {
            updata (j, i);
            if (j * j != a) updata (a / j, i);
        }
    }
    for (int i = 1; i < N; i++) {
        st.insert (pii (i, i - 1));
    }
    LL res = 0;
    for (int i = N - 1; i >= 1; i--) {
        LL tmp = area;
        if (l[0][i] && r[0][i] && l[0][i] != r[0][i]) {
//            cout << i << ' ' << l[0][i] << ' ' << l[1][i] << ' ' << r[0][i] << ' ' << r[1][i] << endl;
            solve (l[1][i] + 1, n);
            solve (1, r[1][i] - 1);
            solve (l[0][i] + 1, r[0][i] - 1);
        }
        res += (area - tmp) * i;
//        if (area) cout << area << endl;
    }
    cout << res << endl;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
using namespace std;
struct pp
{
	int lc,rc,m,mark;
	long long k;
}p[400010];
int root,pl,n;
vector<int> A[200010];
long long ans;
int _Min(int x,int y){return x<y?x:y;}
void bt(int l,int r)
{
	int x=++pl,mid=(l+r)>>1;
	p[x].m=l-1;
	p[x].k=(long long)(l+r-2)*(r-l+1)/2;
	if(l<r)
	{
	  p[x].lc=pl+1;bt(l,mid);
	  p[x].rc=pl+1;bt(mid+1,r);
	}
}
void Mark(int x,int l,int r,int k)
{
	p[x].mark=p[x].m=k;
	p[x].k=(long long)(r-l+1)*k;
}
void down(int x,int l,int r)
{
	int mid=(l+r)>>1;
	Mark(p[x].lc,l,mid,p[x].mark);
	Mark(p[x].rc,mid+1,r,p[x].mark);
	p[x].mark=0;
}
int Find(int x,int l,int r,int k)
{
	if(l==r)return l;
	int mid=(l+r)>>1;
	if(p[x].mark)down(x,l,r);
	if(p[p[x].rc].m<k)return Find(p[x].rc,mid+1,r,k);
	return Find(p[x].lc,l,mid,k);
}
long long query(int x,int l,int r,int ll,int rr)
{
	if(l==ll&&r==rr)return p[x].k;
	int mid=(l+r)>>1;
	if(p[x].mark)down(x,l,r);
	if(rr<=mid)return query(p[x].lc,l,mid,ll,rr);
	if(ll>mid)return query(p[x].rc,mid+1,r,ll,rr);
	return query(p[x].lc,l,mid,ll,mid)+query(p[x].rc,mid+1,r,mid+1,rr);
}
void build(int x,int l,int r,int ll,int rr,int k)
{
	if(l==ll&&r==rr){Mark(x,l,r,k);return;}
	int mid=(l+r)>>1;
	if(p[x].mark)down(x,l,r);
	if(rr<=mid)build(p[x].lc,l,mid,ll,rr,k);
	else if(ll>mid)build(p[x].rc,mid+1,r,ll,rr,k);
	else{build(p[x].lc,l,mid,ll,mid,k);build(p[x].rc,mid+1,r,mid+1,rr,k);}
	p[x].k=p[p[x].lc].k+p[p[x].rc].k;
	p[x].m=_Min(p[p[x].lc].m,p[p[x].rc].m);
}
void deal(int l,int r,int k)
{
	if(l>r)return;
	int L;
	if(p[root].m>=r)L=0;
	else L=Find(root,1,n,r);
	if(l<=L)
	{
	  ans+=((long long)(L-l+1)*r-query(root,1,n,l,L))*k;
	  build(root,1,n,l,L,r);
	}
}
int main()
{
	int i,j,x,s;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
	  scanf("%d",&x);
	  for(j=1;j*j<=x;j++)
	    if(x%j==0)
		{
		  A[j].push_back(i);
		  if(j*j!=x)A[x/j].push_back(i);
		}
	}
	root=1;
	bt(1,n);
	for(i=200000;i>=1;i--)
	{
	  if(A[i].size()<2)continue;
	  s=A[i].size();
	  deal(1,A[i][s-2]-1,i);
	  deal(A[i][1]+1,n,i);
	  deal(A[i][0]+1,A[i][s-1]-1,i);
	}
	printf("%I64d\n",ans);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;

const int N=200010;
struct node{
	int mn,mx,tag;
	long long sum;
}t[N*4];
vector<int>v[N];
int a[N],x,n;
long long h[N],ans;

void updata(int k,int x,int len){
	t[k].mn=t[k].mx=t[k].tag=x;
	t[k].sum=1ll*len*x;
}
void modify(int k,int l,int r,int ql,int qr,int x){
	if(ql<=l&&qr>=r&&t[k].mx<=x){
		t[k].tag=t[k].mx=t[k].mn=x;
		t[k].sum=1ll*(r-l+1)*x;
		return;
	}
	if(ql>r||qr<l||t[k].mn>=x||l==r) return;
	int mid=(l+r)>>1;
	if(t[k].tag){
		updata(k*2,t[k].tag,mid-l+1);
		updata(k*2+1,t[k].tag,r-mid);
		t[k].tag=0;
	}
	modify(k*2,l,mid,ql,qr,x);
	modify(k*2+1,mid+1,r,ql,qr,x);
	t[k].mn=min(t[k*2].mn,t[k*2+1].mn);
	t[k].mx=max(t[k*2].mx,t[k*2+1].mx);
	t[k].sum=t[k*2].sum+t[k*2+1].sum;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	for(int i=1;i<=n;i++){
		for(int j=1;j*j<=a[i];j++)
		if(a[i]%j==0){
			v[j].push_back(i);
			if(a[i]/j!=j) v[a[i]/j].push_back(i);
		}
	}
	for(int i=1;i<=n;i++) modify(1,1,n,i,i,i);
	for(int i=200000;i;i--){
		h[i]=1ll*(n+1)*n-t[1].sum;
		if((x=v[i].size())<2) continue;
		modify(1,1,n,1,v[i][0],v[i][x-2]);
		modify(1,1,n,v[i][0]+1,v[i][1],v[i][x-1]);
		modify(1,1,n,v[i][1]+1,n,n+1);
	}
	h[0]=1ll*(n+1)*n-t[1].sum;
	for(int i=1;i<=200000;i++) ans+=(h[i]-h[i-1])*i;
	printf("%I64d\n",ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;

const int o = 1 << 18;
const int maxn = 200005;
const int inf = 0x3f3f3f3f;

struct tournament {
	ll sum[o * 2];
	int prop[o * 2], maxi[o * 2];
	void propag(int x, int lo, int hi) {
		if (prop[x] == 0)
			return;
		maxi[x] = prop[x];
		sum[x] = (ll)(hi - lo) * prop[x];
		if (x < o) {
			prop[x * 2] = prop[x];
			prop[x * 2 + 1] = prop[x];
		}
		prop[x] = 0;
	}
	void update(int x, int lo, int hi, int qlo, int qhi, int val) {
		propag(x, lo, hi);
		if (lo >= qlo && hi <= qhi) {
			prop[x] = val;
			propag(x, lo, hi);
			return;
		}
		if (lo >= qhi || hi <= qlo)
			return;
		int mid = (lo + hi) / 2;
		update(x * 2, lo, mid, qlo, qhi, val);
		update(x * 2 + 1, mid, hi, qlo, qhi, val);
		sum[x] = sum[x * 2] + sum[x * 2 + 1];
		maxi[x] = max(maxi[x * 2], maxi[x * 2 + 1]);
	}
	ll query_sum(int x, int lo, int hi, int qlo, int qhi) {
		propag(x, lo, hi);
		if (lo >= qlo && hi <= qhi)
			return sum[x];
		if (lo >= qhi || hi <= qlo)
			return 0;
		int mid = (lo + hi) / 2;
		ll L = query_sum(x * 2, lo, mid, qlo, qhi);
		ll R = query_sum(x * 2 + 1, mid, hi, qlo, qhi);
		return L + R;
	}
	int query_fbte(int x, int lo, int hi, int val) {
		propag(x, lo, hi);
		if (maxi[x] < val)
			return inf;
		if (lo == hi - 1)
			return x - o;
		int mid = (lo + hi) / 2;
		propag(x * 2, lo, mid);
		if (maxi[x * 2] < val)
			return query_fbte(x * 2 + 1, mid, hi, val);
		return query_fbte(x * 2, lo, mid, val);
	}
} t;

int n;

int f[maxn], l[maxn];
int f2[maxn], l2[maxn];

ll sol;

void calc(int lo, int hi, int val) {
	int tw = t.query_fbte(1, 0, o, hi) - 1;
	if (tw >= hi)
		tw = hi - 1;
	if (tw >= lo) {
		sol += (ll)val * ((ll)hi * (tw - lo + 1) - t.query_sum(1, 0, o, lo, tw + 1));
		t.update(1, 0, o, lo, tw + 1, hi);
	}
}

int main (void) {
	scanf("%d", &n);
	memset(f, inf, sizeof f);
	memset(l, -1, sizeof l);
	for (int i = 1; i <= n; i++) {
		int x;
		scanf("%d", &x);
		if (f[x] > i) {
			f2[x] = f[x];
			f[x] = i;
		}
		if (l[x] < i) {
			l2[x] = l[x];
			l[x] = i;
		}
	}
	t.update(1, 0, o, 0, 1, 1);
	for (int i = 1; i <= n; i++)
		t.update(1, 0, o, i, i + 1, i + 1);
	for (int i = maxn - 5; i > 0; i--) {
		int lo = inf;
		int hi = -1;
		for (int j = i; j < maxn; j += i) {
			lo = min(lo, f[j]);
			hi = max(hi, l[j]);
		}
		if (lo < n && hi > 1 && hi - lo > 1)
			calc(lo, hi, i);
		else
			continue;
		int llo = inf, hhi = -1;
		for (int j = i; j < maxn; j += i) {
			if (f[j] == lo)
				llo = min(llo, f2[j]);
			else
				llo = min(llo, f[j]);
			if (l[j] == hi)
				hhi = max(hhi, l2[j]);
			else
				hhi = max(hhi, l[j]);
		}
		if (llo < n)
			calc(llo, n + 1, i);
		if (hhi > 0)
			calc(0, hhi, i);

	}
	printf("%lld\n", sol);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;

const int o = 1 << 18;
const int maxn = 200005;
const int inf = 0x3f3f3f3f;

struct tournament {
	ll sum[o * 2];
	int prop[o * 2], maxi[o * 2];
	void propag(int x, int lo, int hi) {
		if (prop[x] == 0)
			return;
		maxi[x] = prop[x];
		sum[x] = (ll)(hi - lo) * prop[x];
		if (x < o) {
			prop[x * 2] = prop[x];
			prop[x * 2 + 1] = prop[x];
		}
		prop[x] = 0;
	}
	void update(int x, int lo, int hi, int qlo, int qhi, int val) {
		propag(x, lo, hi);
		if (lo >= qlo && hi <= qhi) {
			prop[x] = val;
			propag(x, lo, hi);
			return;
		}
		if (lo >= qhi || hi <= qlo)
			return;
		int mid = (lo + hi) / 2;
		update(x * 2, lo, mid, qlo, qhi, val);
		update(x * 2 + 1, mid, hi, qlo, qhi, val);
		sum[x] = sum[x * 2] + sum[x * 2 + 1];
		maxi[x] = max(maxi[x * 2], maxi[x * 2 + 1]);
	}
	ll query_sum(int x, int lo, int hi, int qlo, int qhi) {
		propag(x, lo, hi);
		if (lo >= qlo && hi <= qhi)
			return sum[x];
		if (lo >= qhi || hi <= qlo)
			return 0;
		int mid = (lo + hi) / 2;
		ll L = query_sum(x * 2, lo, mid, qlo, qhi);
		ll R = query_sum(x * 2 + 1, mid, hi, qlo, qhi);
		return L + R;
	}
	int query_fbte(int x, int lo, int hi, int val) {
		propag(x, lo, hi);
		if (maxi[x] < val)
			return inf;
		if (lo == hi - 1)
			return x - o;
		int mid = (lo + hi) / 2;
		propag(x * 2, lo, mid);
		if (maxi[x * 2] < val)
			return query_fbte(x * 2 + 1, mid, hi, val);
		return query_fbte(x * 2, lo, mid, val);
	}
} t;

int n;

int f[maxn], l[maxn];
int f2[maxn], l2[maxn];

ll sol;

void calc(int lo, int hi, int val) {
	int tw = t.query_fbte(1, 0, o, hi) - 1;
	if (tw >= hi)
		tw = hi - 1;
	if (tw >= lo) {
		sol += (ll)val * ((ll)hi * (tw - lo + 1) - t.query_sum(1, 0, o, lo, tw + 1));
		t.update(1, 0, o, lo, tw + 1, hi);
	}
}

int main (void) {
	scanf("%d", &n);
	memset(f, inf, sizeof f);
	memset(l, -1, sizeof l);
	for (int i = 1; i <= n; i++) {
		int x;
		scanf("%d", &x);
		if (f[x] > i) {
			f2[x] = f[x];
			f[x] = i;
		}
		if (l[x] < i) {
			l2[x] = l[x];
			l[x] = i;
		}
	}
	t.update(1, 0, o, 0, 1, 1);
	for (int i = 1; i <= n; i++)
		t.update(1, 0, o, i, i + 1, i + 1);
	for (int i = maxn - 5; i > 0; i--) {
		int lo = inf;
		int hi = -1;
		for (int j = i; j < maxn; j += i) {
			lo = min(lo, f[j]);
			hi = max(hi, l[j]);
		}
		if (lo < n && hi > 1)
			calc(lo, hi, i);
		else
			continue;
		int llo = inf, hhi = -1;
		for (int j = i; j < maxn; j += i) {
			if (f[j] == lo)
				llo = min(llo, f2[j]);
			else
				llo = min(llo, f[j]);
			if (l[j] == hi)
				hhi = max(hhi, l2[j]);
			else
				hhi = max(hhi, l[j]);
		}
		if (llo < n)
			calc(llo, n + 1, i);
		if (hhi > 1)
			calc(0, hhi, i);

	}
	printf("%lld\n", sol);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int M = 200010;
struct node
{
	int tag,mx,mn;
	long long sum;
}t[M<<2];
vector<int>v[M];
int a[M],x,n;
long long h[M],ans;
void pushdown(int k,int x,int len)
{
	t[k].mx=t[k].mn=t[k].tag=x;
	t[k].sum=1ll*len*x;
}
void update(int k,int l,int r,int ql,int qr,int x)
{
	if(ql<=l&&qr>=r&&t[k].mx<=x)
	{
		t[k].tag=t[k].mx=t[k].mn=x;
		t[k].sum=1ll*(r-l+1)*x;
		return;
	}
	if(ql>r||qr<l||t[k].mn>=x||l==r) return;
	int mid=(l+r)>>1;
	if(t[k].tag)
	{
		pushdown(k*2,t[k].tag,mid-l+1);
		pushdown(k*2+1,t[k].tag,r-mid);
		t[k].tag=0;
	}
	update(k*2,l,mid,ql,qr,x);
	update(k*2+1,mid+1,r,ql,qr,x);
	t[k].mn=min(t[k*2].mn,t[k*2+1].mn);
	t[k].mx=max(t[k*2].mx,t[k*2+1].mx);
	t[k].sum=t[k*2].sum+t[k*2+1].sum;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
		for(int j=1;j*j<a[i];j++)
			if(a[i]%j==0)
			{
				v[j].push_back(i);
				if(a[i]/j!=j)
					v[a[i]/j].push_back(i);
			}
	for(int i=1;i<=n;i++)
		update(1,1,n,i,i,i);
	for(int i=20000;i;i--)
	{
		h[i]=1ll*(n+1)*n-t[1].sum;
		if((x=v[i].size())<2)
			continue;
		update(1,1,n,1,v[i][0],v[i][x-2]);
		update(1,1,n,v[i][0]+1,v[i][1],v[i][x-1]);
		update(1,1,n,v[i][1]+1,n,n+1);
	}
	h[0]=1ll*(n+1)*n-t[1].sum;
	for(int i=20000;i;i--)
		ans+=(h[i]-h[i-1])*i;
	printf("%I64d\n",ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int M = 200010;
struct node
{
	int tag,mx,mn;
	long long sum;
}t[M<<2];
vector<int>v[M];
int a[M],x,n;
long long h[M],ans;
void pushdown(int k,int x,int len)
{
	t[k].mx=t[k].mn=t[k].tag=x;
	t[k].sum=1ll*len*x;
}
void update(int k,int l,int r,int ql,int qr,int x)
{
	if(ql<=l&&qr>=r&&t[k].mx<=x)
	{
		t[k].tag=t[k].mx=t[k].mn=x;
		t[k].sum=1ll*(r-l+1)*x;
		return;
	}
	if(ql>r||qr<l||t[k].mn>=x||l==r) return;
	int mid=(l+r)>>1;
	if(t[k].tag)
	{
		pushdown(k*2,t[k].tag,mid-l+1);
		pushdown(k*2+1,t[k].tag,r-mid);
		t[k].tag=0;
	}
	update(k*2,l,mid,ql,qr,x);
	update(k*2+1,mid+1,r,ql,qr,x);
	t[k].mn=min(t[k*2].mn,t[k*2+1].mn);
	t[k].mx=max(t[k*2].mx,t[k*2+1].mx);
	t[k].sum=t[k*2].sum+t[k*2+1].sum;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
		for(int j=1;j*j<=a[i];j++)
			if(a[i]%j==0)
			{
				v[j].push_back(i);
				if(a[i]/j!=j)
					v[a[i]/j].push_back(i);
			}
	for(int i=1;i<=n;i++)
		update(1,1,n,i,i,i);
	for(int i=20000;i;i--)
	{
		h[i]=1ll*(n+1)*n-t[1].sum;
		if((x=v[i].size())<2)
			continue;
		update(1,1,n,1,v[i][0],v[i][x-2]);
		update(1,1,n,v[i][0]+1,v[i][1],v[i][x-1]);
		update(1,1,n,v[i][1]+1,n,n+1);
	}
	h[0]=1ll*(n+1)*n-t[1].sum;
	for(int i=20000;i;i--)
		ans+=(h[i]-h[i-1])*i;
	printf("%I64d\n",ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int M = 200010;
struct node
{
	int tag,mx,mn;
	long long sum;
}t[M<<2];
vector<int>v[M];
int a[M],x,n;
long long h[M],ans;
void pushdown(int k,int x,int len)
{
	t[k].mx=t[k].mn=t[k].tag=x;
	t[k].sum=1ll*len*x;
}
void update(int k,int l,int r,int ql,int qr,int x)
{
	if(ql<=l&&qr>=r&&t[k].mx<=x)
	{
		t[k].tag=t[k].mx=t[k].mn=x;
		t[k].sum=1ll*(r-l+1)*x;
		return;
	}
	if(ql>r||qr<l||t[k].mn>=x||l==r) return;
	int mid=(l+r)>>1;
	if(t[k].tag)
	{
		pushdown(k*2,t[k].tag,mid-l+1);
		pushdown(k*2+1,t[k].tag,r-mid);
		t[k].tag=0;
	}
	update(k*2,l,mid,ql,qr,x);
	update(k*2+1,mid+1,r,ql,qr,x);
	t[k].mn=min(t[k*2].mn,t[k*2+1].mn);
	t[k].mx=max(t[k*2].mx,t[k*2+1].mx);
	t[k].sum=t[k*2].sum+t[k*2+1].sum;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
		for(int j=1;j*j<=a[i];j++)
			if(a[i]%j==0)
			{
				v[j].push_back(i);
				if(a[i]/j!=j)
					v[a[i]/j].push_back(i);
			}
	for(int i=1;i<=n;i++)
		update(1,1,n,i,i,i);
	for(int i=200000;i;i--)
	{
		h[i]=1ll*(n+1)*n-t[1].sum;
		if((x=v[i].size())<2)
			continue;
		update(1,1,n,1,v[i][0],v[i][x-2]);
		update(1,1,n,v[i][0]+1,v[i][1],v[i][x-1]);
		update(1,1,n,v[i][1]+1,n,n+1);
	}
	h[0]=1ll*(n+1)*n-t[1].sum;
	for(int i=200000;i;i--)
		ans+=(h[i]-h[i-1])*i;
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 200010;

int n, a[N];
long long t[N<<2];
int lz[N<<2], mx[N<<2];
long long ans, f[N];
vector <int> dvs[N];
#define le (v << 1)
#define ri ((v << 1) | 1)
#define mid ((l + r) >> 1)

void lazy(int v, int l, int r) {
	if (!lz[v]) return;
	if (l != r) lz[le] = lz[v], lz[ri] = lz[v];
	mx[v] = lz[v], t[v] = 1ll * (r - l + 1) * lz[v];
	lz[v] = 0;
}

void upd(int v, int l, int r, int L, int R, int val) {
	lazy(v, l, r);
	if (l > r || L > r || R < l) return;
	if (L <= l && r <= R) { lz[v] = val; lazy(v, l, r); return; }
	upd(le, l, mid, L, R, val); upd(ri, mid + 1, r, L, R, val);
	t[v] = t[le] + t[ri];
	mx[v] = max(mx[le], mx[ri]);
}

void build(int v, int l, int r) {
	if (l > r) return;
	if (l == r) { t[v] = mx[v] = l; return; }
	build(le, l, mid); build(ri, mid + 1, r);
	t[v] = t[le] + t[ri];
	mx[v] = max(mx[le], mx[ri]);
}

int find(int v, int l, int r, int val) {
	lazy(v, l, r);
	if (l > r) return 0;
	if (l == r)
		if (mx[v] <= val) return l;
		else return 0;
	lazy(le, l, mid); lazy(ri, mid + 1, r);

	if (mx[le] <= val) return max(mid, find(ri, mid + 1, r, val));
	else return find(le, l, mid, val);
}

void solve(int l, int r, int val) {
	int pos = find(1, 1, n, val);
	if (pos) upd(1, 1, n, l, min(r, pos), val);
}

int main() {
	scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j * j <= a[i]; j++) if (a[i] % j == 0) {
			if (j * j != a[i]) dvs[j].push_back(i), dvs[a[i]/j].push_back(i);
			else dvs[j].push_back(i);
		}
	}

	build(1, 1, n);
	for (int i = N-1; i >= 0; i--) {
		f[i] = 1ll * n * (n + 1) - t[1];
		if (dvs[i].size() >= 2) {
			solve(1, dvs[i][0], dvs[i][dvs[i].size()-2]);
			solve(dvs[i][0]+1, dvs[i][1], dvs[i][dvs[i].size()-1]);
			solve(dvs[i][1]+1, n, n+1);
		}
	}

	for (int i = N-1; i >= 1; i--) {
		long long res = f[i] - f[i-1];
		ans += res * i;
	}

	printf("%lld\n", ans);
}


#include <bits/stdc++.h>
#include <map>
#define sqr(x) (x)*(x)
#define eps 1e-12
using namespace std;
typedef long long ll;

const int N=200010;
int a[N],pos[N],n;
ll ans,sum;
struct Point
{
	int l,r,k;
}p[N*3];
map<ll ,ll > m;
map<ll ,ll >::iterator it,it3;
bool cmp(struct Point a ,struct Point b) {return a.r>b.r;}

void del(map<ll ,ll >::iterator it2)
{
	//cout<<it2->second<<' '<<m.size()<<endl;
	it=m.end();
	it--;
	if(it->first==it2->first)
	{
		//cout<<"yyy"<<endl;
		if(it2==m.begin())
		{
			sum+=it2->second*it2->first;
			m.erase(it2);
		}else{
			it2--;
			sum-=it->first*(it2->second-it->second);
			m.erase(it);
		}
	}
	else
	{
		if(it2!=m.begin()){
			it=it2;
			it++;
			sum-=it2->second*(it->first-it2->first);
			ll t=it2->first;
			it2--;
			sum+=it2->second*(it->first-t);
			m.erase(++it2);
		}else{
			it=it2;
			it++;
			sum-=it2->second*(it->first-it2->first);
			m.erase(it2);
		}
	}
}
void ins2(int j)
{
	//cout<<j<<endl;
	it=m.find(p[j].l);
	if(m.upper_bound(it->first)==m.end()){
	if(it==m.begin()) sum-=it->first*it->second;
	else{
		it3=it;
		it3--;
		sum+=it3->second*it->first-it->second*it->first;
		}
	}else {
		if(it==m.begin()){
			it3=it;
			it3++;
			sum+=it->second*(it3->first-it->first);
		}else{
			it3=it;
			it3++;
			ll t=it3->first;
			sum+=it->second*(it3->first-it->first);
			it3=it;
			it3--;
			sum-=it3->second*(t-it->first);
		}
	}
}
void ins(int j)
{
	bool pd=false;
	if(m.count(p[j].l)==0){
		it=m.lower_bound(p[j].l);
		if(it==m.begin() || (--it)->second<p[j].k) {
			m[p[j].l]=p[j].k;
			pd=true;
			ins2(j);

		}
	} else if (m[p[j].l]<p[j].k)
	{
		it=m.find(p[j].l);
		pd=true;
		del(it);
		m[p[j].l]=p[j].k;
		ins2(j);
	}
	if(pd){
		for(it=m.find(p[j].l),it++;it!=m.end() && it->second<=p[j].k;it=m.find(p[j].l),it++) del(it);
	}
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	for(int i=1;i<=n;i++) pos[a[i]]=i;
	for(int i=200000;i>=1;i--)
	{
		p[3*i-1].l=p[3*i-1].r=n+1;
		p[3*i-2].l=p[3*i-2].r=0;
		p[3*i].l=n+1;
		p[3*i].r=0;
		p[3*i-1].k=p[3*i-2].k=p[3*i].k=i;
		for(int j=1;j*i<=200000;j++) if(pos[j*i]!=0)
		{
			if(p[3*i].l!=n+1) p[3*i-1].l=min(p[3*i-1].l,(pos[i*j]<p[3*i].l?p[3*i].l:pos[i*j]));
			if(p[3*i].r!=0) p[3*i-2].r=max(p[3*i-2].r,(pos[i*j]>p[3*i].r?p[3*i].r:pos[i*j]));
			p[3*i].r=max(p[3*i].r,pos[i*j]);
			p[3*i].l=min(p[3*i].l,pos[i*j]);
		}
	}
	sort(p+1,p+600001,cmp);
	p[0].r=n+1;
	p[0].l=0;
	p[0].k=1;
	int j=1;
	//cout<<"sofsidf"<<endl;
	for(ll i=n+1;i>=2;i--)
	{
		for(;j<=600000 && p[j].r>=i;j++)
		{
			//if(p[j].r<n+1) cout<<p[j].l<<' '<<p[j].r<<' '<<p[j].k<<' '<<sum<<endl;
			ins(j);
		}
		//for(int i=0;i<=n;i++) if(m.count(i)!=0) cout<<i<<' '<<m[i]<<endl;
		//cout<<sum<<endl;
		for(it=m.end();m.begin()!=m.end() && (--it)->first>=i-1;it=m.end()) del(it);
		it=m.end();
		if(it!=m.begin()){
		it--;
		ans+=sum+it->second*(i-1);
		}
		//cout<<"yyy "<<ans<<endl;
	}
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#include <map>
#define sqr(x) (x)*(x)
#define eps 1e-12
using namespace std;
typedef long long ll;

const int N=200010;
int a[N],pos[N],n;
ll ans,sum;
struct Point
{
	int l,r,k;
}p[N*3];
map<ll ,ll > m;
map<ll ,ll >::iterator it,it3;
bool cmp(struct Point a ,struct Point b) {return a.r>b.r;}

void del(map<ll ,ll >::iterator it2)
{
	//cout<<it2->second<<' '<<m.size()<<endl;
	it=m.end();
	it--;
	if(it->first==it2->first)
	{
		//cout<<"yyy"<<endl;
		if(it2==m.begin())
		{
			sum+=it2->second*it2->first;
			m.erase(it2);
		}else{
			it2--;
			sum-=it->first*(it2->second-it->second);
			m.erase(it);
		}
	}
	else
	{
		if(it2!=m.begin()){
			it=it2;
			it++;
			sum-=it2->second*(it->first-it2->first);
			ll t=it2->first;
			it2--;
			sum+=it2->second*(it->first-t);
			m.erase(it2++);
		}else{
			it=it2;
			it++;
			sum-=it2->second*(it->first-it2->first);
			m.erase(it2);
		}
	}
}
void ins2(int j)
{
	//cout<<j<<endl;
	it=m.find(p[j].l);
	if(m.upper_bound(it->first)==m.end()){
	if(it==m.begin()) sum-=it->first*it->second;
	else{
		it3=it;
		it3--;
		sum+=it3->second*it->first-it->second*it->first;
		}
	}else {
		if(it==m.begin()){
			it3=it;
			it3++;
			sum+=it->second*(it3->first-it->first);
		}else{
			it3=it;
			it3++;
			ll t=it3->first;
			sum+=it->second*(it3->first-it->first);
			it3=it;
			it3--;
			sum-=it3->second*(t-it->first);
		}
	}
}
void ins(int j)
{
	//ll last=0;
	bool pd=false;
	if(m.count(p[j].l)==0){
		it=m.lower_bound(p[j].l);
		if(it==m.begin() || (--it)->second<p[j].k) {
			m[p[j].l]=p[j].k;
			pd=true;
			ins2(j);

		}
	} else if (m[p[j].l]<p[j].k)
	{
		//last=m[p[j].l];
		//cout<<"yyy"<<endl;
		it=m.find(p[j].l);
		pd=true;
		del(it);
		m[p[j].l]=p[j].k;
		ins2(j);
	}
	if(pd){
		for(it=m.find(p[j].l),it++;it!=m.end() && it->second<=p[j].k;it=m.find(p[j].l),it++/*,cout<<"YYY"<<endl*/)
		{
			//cout<<it->second<<' '<<p[j].k<<endl;
			del(it);
		}
	}

}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	for(int i=1;i<=n;i++) pos[a[i]]=i;
	for(int i=200000;i>=1;i--)
	{
		p[3*i-1].l=p[3*i-2].l=p[3*i].l=N;
		p[3*i-1].r=p[3*i-2].r=p[3*i].r=0;
		p[3*i-1].k=p[3*i-2].k=p[3*i].k=i;
		for(int j=1;j*i<=200000;j++) if(pos[j*i]!=0)
		{
			p[3*i].r=max(p[3*i].r,pos[i*j]);
			p[3*i].l=min(p[3*i].l,pos[i*j]);
		}
		if(p[3*i].l<p[3*i].r){
			p[3*i-1].l=p[3*i].r;
			p[3*i-1].r=n+1;
			p[3*i-2].l=0;
			p[3*i-2].r=p[3*i].l;
		}
	}
	sort(p+1,p+600001,cmp);
	p[0].r=n+1;
	p[0].l=0;
	p[0].k=1;
	int j=1;
	//cout<<"sofsidf"<<endl;
	for(int i=n+1;i>=2;i--)
	{
		for(;j<=600000 && p[j].r>=i;j++)
		{
			//cout<<p[j].l<<' '<<p[j].r<<' '<<p[j].k<<endl;

			ins(j);
			//cout<<sum<<endl;
		}
		//cout<<i<<endl;
		//cout<<m[2]<<endl;

		for(it=m.end(),it--;it->first>=i-1;it=m.end(),it--) del(it);
		//cout<<sum<<endl;
		it=m.end();
		it--;
		ans+=sum+it->second*(i-1);
		//cout<<ans<<endl;
	}
	printf("%I64d\n",ans);
	return 0;
}


#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

typedef long long int ll;

//#include "alerta.h"
const int limite=400005;
const int limdiv=200005;
//const int limite=100;

int maximo[limite];

void inserta(int pos,int val)
{
  //cout<<"inserta "<<pos<<" "<<val<<endl;
  for (;0<pos;pos-=pos&-pos)
    maximo[pos]=max(maximo[pos],val);
}

int maxri(int pos)
{
  int maxi=0;
  for (;pos<limite;pos+=pos&-pos)
    maxi=max(maxi,maximo[pos]);
  //cout<<"maxri "<<pos<<" "<<maxi<<endl;
  return maxi;
}

int n;
vector<int> divisores[limdiv];
int a[limite];
int firstpos[limite],lastpos[limite];
ll sol=0;

void calcula()
{
  for (int i=1;i<n+n;i++) {
    //cout<<"caso "<<i<<" "<<a[i]<<endl;
    vector<int> &vd=divisores[a[i]];
    for (int j=0;j<int(vd.size());j++) {
      int d=vd[j];
      if (firstpos[d]) inserta(d,lastpos[d]);
    }
    for (int j=0;j<int(vd.size());j++) {
      int d=vd[j];
      if (firstpos[d]) {
	if (n<=i) {
	  int sup=min(lastpos[d],n+1);
	  int inf=max(max(firstpos[d],maxri(d+1)+1),i-(n-1)+1);
	  //cout<<"divisor "<<d<<" "<<inf<<" "<<sup<<endl;
	  if (inf<=sup) sol+=d*(sup-inf+1);
	}
	lastpos[d]=i;
      } else {
	firstpos[d]=lastpos[d]=i;
      }
    }
  }
}

int main()
{
  for (int i=1;i<limdiv;i++)
    for (int j=i;j<limdiv;j+=i)
      divisores[j].push_back(i);
  ios::sync_with_stdio(false);
  cin>>n;
  for (int i=1;i<=n;i++)
    cin>>a[i];
  for (int i=1;i<=n;i++)
    a[i+n]=a[i];
  calcula();
  cout<<sol<<endl;
}


#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

typedef long long int ll;

const int tope=1<<19;
const int primero=1<<18;

int len[tope];
int lenle[tope];
ll valor[tope];
ll suma[tope];

void poner(int pos,int val,int inlen)
{
  pos+=primero;
  len[pos]=inlen;
  valor[pos]=val;
  suma[pos]=val;
  if (val==0)
    lenle[pos]=len[pos];
  else
    lenle[pos]=0;
  while (pos>1) {
    pos/=2;
    len[pos]=len[2*pos]+len[2*pos+1];
    valor[pos]=max(valor[2*pos],valor[2*pos+1]);
    if (valor[2*pos]==0) {
      lenle[pos]=len[2*pos]+lenle[2*pos+1];
      suma[pos]=suma[2*pos+1];
    } else {
      lenle[pos]=lenle[2*pos];
      suma[pos]=suma[2*pos]+suma[2*pos+1]+valor[2*pos]*lenle[2*pos+1];
    }
  }
}

int siguiente(int pos)
{
  pos+=primero;
  if (valor[pos]) return pos-primero;
  while (pos>1) {
    if (pos%2==0 and valor[pos+1]) {
      pos++;
      while (pos<primero)
	if (valor[2*pos]) pos=2*pos;
	else pos=2*pos+1;
      return pos-primero;
    }
    pos/=2;
  }
  return 0;
}

void inserta(int posini,int val)
{
  {
    int pos=posini+primero;
    if (val<=valor[pos]) return;
    while (pos>1) {
      if (pos%2==1 and val<=valor[pos-1]) return;
      pos/=2;
    }
  }
  int pos=posini;
  poner(pos,val,1);
  pos++;
  for (;;) {
    pos=siguiente(pos);
    if (pos==0 or val<valor[pos+primero]) return;
    poner(pos,0,1);
  }
}

const int limite=200005;

int n;
vector<int> divisores[limite];
int a[limite];
int firstpos[limite];
int visto[limite];
ll sol=0;

void calcula()
{
  for (int i=1;i<n;i++)
    poner(i,0,1);
  for (int i=1;i<n;i++) {
    int valorponer=0;
    vector<int> &vd=divisores[a[i]];
    for (int j=0;j<int(vd.size());j++) {
      int d=vd[j];
      visto[d]++;
      if (visto[d]==1) firstpos[d]=i;
      else if (visto[d]==2) valorponer=d;
    }
    if (valorponer) inserta(i,valorponer);
  }
  sol+=suma[1];
  for (int i=0;i<limite;i++)
    visto[i]=0;
  for (int i=n;i>1;i--) {
    poner(i-1,0,0);
    vector<int> &vd=divisores[a[i]];
    for (int j=0;j<int(vd.size());j++) {
      int d=vd[j];
      visto[d]++;
      if (visto[d]==1 and firstpos[d] and firstpos[d]<i-1)
	inserta(firstpos[d],d);
      else if (visto[d]==2)
	inserta(0,d);
    }
    sol+=suma[1];
  }
}

int main()
{
  for (int i=1;i<limite;i++)
    for (int j=i;j<limite;j+=i)
      divisores[j].push_back(i);
  ios::sync_with_stdio(false);
  cin>>n;
  for (int i=1;i<=n;i++)
    cin>>a[i];
  calcula();
  cout<<sol<<endl;
}


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

const int MAXN = 200005;

long long tree[4*MAXN];
int L[4*MAXN];
int R[4*MAXN];
int lazy[4*MAXN];

int setVal(int i, int l, int r, int x, int y, int val)
{
	if (x > y)
		return 0;
	if (l >= x && r <= y)
	{
		if (R[i] >= val)
			return tree[i];
		if (L[i] <= val)
		{
			lazy[i] = val;
			tree[i] = (r - l + 1) * 1ll * val;
			L[i] = R[i] = val;
			return tree[i];
		}
	}
	if (l > y || r < x)
		return tree[i];

	int mid = (l + r)/2;
	if (lazy[i] != -1)
	{
		setVal(i*2 + 1, l, mid, l, mid, lazy[i]);
		setVal(i*2 + 2, mid+1, r, mid+1, r, lazy[i]);
		lazy[i] = -1;
	}
	tree[i] = setVal(i*2 + 1, l, mid, x, y, val) + setVal(i*2 + 2, mid+1, r, x, y, val);
	L[i] = L[i*2 + 1];
	R[i] = R[i*2 + 1];
	return tree[i];
}

long long sum(int i, int l, int r, int x, int y)
{
	if (x > y)
		return 0;
	if (l >= x && r <= y)
	{
		return tree[i];
	}
	if (l > y || r < x)
		return 0;

	int mid = (l + r)/2;
	if (lazy[i] != -1)
	{
		setVal(i*2 + 1, l, mid, l, mid, lazy[i]);
		setVal(i*2 + 2, mid+1, r, mid+1, r, lazy[i]);
		lazy[i] = -1;
	}
	return sum(i*2 + 1, l, mid, x, y) + sum(i*2 + 2, mid+1, r, x, y);
}

vector<int> divs[MAXN];

void init()
{
	memset(lazy, -1, sizeof(lazy));
	memset(tree, 0, sizeof(tree));
	for (int i = 1; i < MAXN; i++)
		for (int j = i; j < MAXN; j += i)
			divs[j].push_back(i);
}

int a[MAXN];
int cnt[MAXN];
int lst[MAXN];
int main()
{
	init();
	memset(cnt, 0, sizeof(cnt));
	memset(lst, -1, sizeof(lst));
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", a+i);
		for (int j = 0; j < divs[a[i]].size(); j++)
			lst[divs[a[i]][j]] = i;
	}
	long long ans = 0;
	int mx = 0;
	for (int i = n-1; i > 0; i--)
	{
		int x = a[i];
		for (int j = 0; j < divs[x].size(); j++)
		{
			if (cnt[divs[x][j]])
				mx = max(mx, divs[x][j]);
			cnt[divs[x][j]]++;
		}
		setVal(0, 0, n-1, i-1,i-1,mx);
	}
	int l = 0;
	memset(cnt, 0, sizeof(cnt));
	for (int i = 0; i < n; i++)
	{
		ans += sum(0, 0, n-1, i, n-1);
		int x = a[i];
		for (int j = 0; j < divs[x].size(); j++)
		{
			if (cnt[divs[x][j]])
				l = max(l, divs[x][j]);
			cnt[divs[x][j]]++;
			setVal(0, 0, n-1, i+1, lst[divs[x][j]]-1, divs[x][j]);
		}
		setVal(0, 0, n-1, i+1, n-1, l);
	}
	printf("%lld\n", ans);
}


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

const int MAXN = 200005;

long long tree[4*MAXN];
int L[4*MAXN];
int R[4*MAXN];
int lazy[4*MAXN];

int setVal(int i, int l, int r, int x, int y, int val)
{
	if (x > y)
		return 0;
	if (l >= x && r <= y)
	{
		if (R[i] >= val)
			return tree[i];
		if (L[i] <= val)
		{
			lazy[i] = val;
			tree[i] = (r - l + 1) * 1ll * val;
			L[i] = R[i] = val;
			return tree[i];
		}
	}
	if (l > y || r < x)
		return tree[i];

	int mid = (l + r)/2;
	if (lazy[i] != -1)
	{
		setVal(i*2 + 1, l, mid, l, mid, lazy[i]);
		setVal(i*2 + 2, mid+1, r, mid+1, r, lazy[i]);
		lazy[i] = -1;
	}
	tree[i] = setVal(i*2 + 1, l, mid, x, y, val) + setVal(i*2 + 2, mid+1, r, x, y, val);
	L[i] = L[i*2 + 1];
	R[i] = R[i*2 + 2];
	return tree[i];
}

long long sum(int i, int l, int r, int x, int y)
{
	if (x > y)
		return 0;
	if (l >= x && r <= y)
	{
		return tree[i];
	}
	if (l > y || r < x)
		return 0;

	int mid = (l + r)/2;
	if (lazy[i] != -1)
	{
		setVal(i*2 + 1, l, mid, l, mid, lazy[i]);
		setVal(i*2 + 2, mid+1, r, mid+1, r, lazy[i]);
		lazy[i] = -1;
	}
	return sum(i*2 + 1, l, mid, x, y) + sum(i*2 + 2, mid+1, r, x, y);
}

vector<int> divs[MAXN];

void init()
{
	memset(lazy, -1, sizeof(lazy));
	memset(tree, 0, sizeof(tree));
	for (int i = 1; i < MAXN; i++)
		for (int j = i; j < MAXN; j += i)
			divs[j].push_back(i);
}

int a[MAXN];
int cnt[MAXN];
int lst[MAXN];
int main()
{
	init();
	memset(cnt, 0, sizeof(cnt));
	memset(lst, -1, sizeof(lst));
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", a+i);
		for (int j = 0; j < divs[a[i]].size(); j++)
			lst[divs[a[i]][j]] = i;
	}
	long long ans = 0;
	int mx = 0;
	for (int i = n-1; i > 0; i--)
	{
		int x = a[i];
		for (int j = 0; j < divs[x].size(); j++)
		{
			if (cnt[divs[x][j]])
				mx = max(mx, divs[x][j]);
			cnt[divs[x][j]]++;
		}
		setVal(0, 0, n-1, i-1,i-1,mx);
	}
	int l = 0;
	memset(cnt, 0, sizeof(cnt));
	for (int i = 0; i < n; i++)
	{
		ans += sum(0, 0, n-1, i, n-1);
		int x = a[i];
		for (int j = 0; j < divs[x].size(); j++)
		{
			if (cnt[divs[x][j]])
				l = max(l, divs[x][j]);
			cnt[divs[x][j]]++;
			setVal(0, 0, n-1, i+1, lst[divs[x][j]]-1, divs[x][j]);
		}
		setVal(0, 0, n-1, i+1, n-1, l);
	}
	printf("%lld\n", ans);
}


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

const int MAXN = 200005;

long long tree[4*MAXN];
int L[4*MAXN];
int R[4*MAXN];
int lazy[4*MAXN];

long long setVal(int i, int l, int r, int x, int y, int val)
{
	if (x > y)
		return 0;
	if (l >= x && r <= y)
	{
		if (R[i] >= val)
			return tree[i];
		if (L[i] <= val)
		{
			lazy[i] = val;
			tree[i] = (r - l + 1) * 1ll * val;
			L[i] = R[i] = val;
			return tree[i];
		}
	}
	if (l > y || r < x)
		return tree[i];

	int mid = (l + r)/2;
	if (lazy[i] != -1)
	{
		setVal(i*2 + 1, l, mid, l, mid, lazy[i]);
		setVal(i*2 + 2, mid+1, r, mid+1, r, lazy[i]);
		lazy[i] = -1;
	}
	tree[i] = setVal(i*2 + 1, l, mid, x, y, val) + setVal(i*2 + 2, mid+1, r, x, y, val);
	L[i] = L[i*2 + 1];
	R[i] = R[i*2 + 2];
	return tree[i];
}

long long sum(int i, int l, int r, int x, int y)
{
	if (x > y)
		return 0;
	if (l >= x && r <= y)
	{
		return tree[i];
	}
	if (l > y || r < x)
		return 0;

	int mid = (l + r)/2;
	if (lazy[i] != -1)
	{
		setVal(i*2 + 1, l, mid, l, mid, lazy[i]);
		setVal(i*2 + 2, mid+1, r, mid+1, r, lazy[i]);
		lazy[i] = -1;
	}
	return sum(i*2 + 1, l, mid, x, y) + sum(i*2 + 2, mid+1, r, x, y);
}

vector<int> divs[MAXN];

void init()
{
	memset(lazy, -1, sizeof(lazy));
	memset(tree, 0, sizeof(tree));
	for (int i = 1; i < MAXN; i++)
		for (int j = i; j < MAXN; j += i)
			divs[j].push_back(i);
}

int a[MAXN];
int cnt[MAXN];
int lst[MAXN];
int main()
{
	init();
	memset(cnt, 0, sizeof(cnt));
	memset(lst, -1, sizeof(lst));
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", a+i);
		for (int j = 0; j < divs[a[i]].size(); j++)
			lst[divs[a[i]][j]] = i;
	}
	long long ans = 0;
	int mx = 0;
	for (int i = n-1; i > 0; i--)
	{
		int x = a[i];
		for (int j = 0; j < divs[x].size(); j++)
		{
			if (cnt[divs[x][j]])
				mx = max(mx, divs[x][j]);
			cnt[divs[x][j]]++;
		}
		setVal(0, 0, n-1, i-1,i-1,mx);
	}
	int l = 0;
	memset(cnt, 0, sizeof(cnt));
	for (int i = 0; i < n; i++)
	{
		ans += sum(0, 0, n-1, i, n-1);
		int x = a[i];
		for (int j = 0; j < divs[x].size(); j++)
		{
			if (cnt[divs[x][j]])
				l = max(l, divs[x][j]);
			cnt[divs[x][j]]++;
			setVal(0, 0, n-1, i+1, lst[divs[x][j]]-1, divs[x][j]);
		}
		setVal(0, 0, n-1, i+1, n-1, l);
	}
	printf("%lld\n", ans);
}


#include<bits/stdc++.h>
using namespace std;
#define sc(a) scanf("%d", &a)
#define sc2(a,b) scanf("%d%d", &a, &b)
#define sc3(a,b,c) scanf("%d%d%d", &a, &b, &c)
#define pri(x) printf("%d\n", x)
#define mp make_pair
#define pb push_back
#define BUFF ios::sync_with_stdio(false);
#define imprime(v) for(int X=0;X<v.size();X++) printf("%d ", v[X]); printf("\n");
#define endl "\n"
const int INF= 0x3f3f3f3f;
const long double pi= acos(-1);
typedef long long int ll;
typedef long double ld;
typedef pair<int,ll> ii;
typedef vector<int> vi;
typedef vector< vector< int > > vvi;
const int MOD=1e9+7;
const ll LINF=0x3f3f3f3f3f3f3f3f;
ll n;
ll resp;
const int MAXN=200005;
int a[MAXN];
int pen[MAXN],ult[MAXN],pri[MAXN],seg[MAXN];
vi quempri[MAXN];
vi quemseg[MAXN];
ll tr[MAXN*4],lazy[MAXN*4];

void arruma(int i, int j)
{
  if(ult[j]>ult[i])
  {
    if(pen[j]>ult[i])
    {
      pen[i]=pen[j];
    }
    else pen[i]=ult[i];
    ult[i]=ult[j];
  }
  else if(ult[j]>pen[i])
  {
    pen[i]=ult[j];
  }
  if(pri[j]<pri[i])
  {
    if(seg[j]<pri[i])
    {
      seg[i]=seg[j];
    }
    else seg[i]=pri[i];
    pri[i]=pri[j];
  }
  else if(pri[j]<seg[i])
  {
    seg[i]=pri[j];
  }
}

void sieve()
{
  for(int i=1;i<=200000;i++)
  {
    for(int j=i+i;j<=200000;j+=i)
    {
      arruma(i,j);
    }
  }
  for(int i=1;i<=200000;i++)
  {
    if(seg[i]!=INF)
    {
      quempri[pri[i]].pb(i);
      quemseg[seg[i]].pb(i);
    }
  }
}

void push(int nd, int b, int e)
{
  if(lazy[nd]!=0)
  {
    ll tam=e-b+1;
    tr[nd]=tam*lazy[nd];
    if(b!=e)
    {
      lazy[nd*2]=lazy[nd*2+1]=lazy[nd];
    }
    lazy[nd]=0;
  }
}

ll query(int nd, int b, int e, int i, int j)
{
  push(nd,b,e);
  if(i>e or j<b) return 0;
  if(b>=i and e<=j) return tr[nd];
  return query(nd*2,b,(b+e)/2,i,j)+query(nd*2+1,(b+e)/2+1,e,i,j);
}

void update(int nd, int b, int e, int i, int j, ll v)
{
  push(nd,b,e);
  if(i>e or j<b) return;
  if(b>=i and e<=j)
  {
    //cout<<"o intervalo "<<b<<" - "<<e<<" vale "<<v<<endl;
    lazy[nd]=v;
    push(nd,b,e);
    return;
  }
  update(nd*2,b,(b+e)/2,i,j,v);
  update(nd*2+1,(b+e)/2+1,e,i,j,v);
  tr[nd]=tr[nd*2]+tr[nd*2+1];
}

ll pegasoma(ll b, ll e)
{
  if(b>e) return 0;
  return query(1,1,n+5,b,e);
}

ll getr(ll b, ll e,ll respant)
{
  if(b==e) return b;
  ll m=(b+e)/2LL;
  if(pegasoma(m,m)>respant) return getr(m+1,e,respant);
  return getr(b,m,respant);
}

void printa_array()
{
  for(int i=1;i<=n;i++)
  {
    cout<<pegasoma(i,i)<<" ";
  }
  cout<<endl;
}

int getl(int b, int e, ll v)
{
  if(b>=e) return b;
  int m=(b+e)/2;
  if(pegasoma(m,m)<v) return getl(b,m,v);
  return getl(m+1,e,v);
}

void atualiza(ll v, int pos)
{
  if(pegasoma(pos,pos)>v) return;
  int l=getl(1,pos,v);
  //cout<<"to colocando "<<v<<" de "<<l<<" a "<<pos<<endl;
  update(1,1,n+5,l,pos,v);
}

void solve()
{
  for(int i=1;i<=200000;i++)
  {

    if(pen[i]!=0)
    {
      atualiza(i,pen[i]-1);
    }
  }
  //cout<<"no inicio , array era :"<<endl;
  //printa_array();
  ll respant=0;
  for(ll l=1;l<=n;l++)
  {
    for(int v : quemseg[l-1])
    {
      respant=max(respant,(ll)v);
    }
    for(int v : quempri[l-1])
    {
   //   cout<<"quempri em "<<l-1<<" = "<<v<<endl;
      atualiza(v,ult[v]-1);
    }
    //cout<<"com l = "<<l<<", o array era :"<<endl;
    //printa_array();
    //cout<<"a respant era "<<respant<<endl;
    ll r=getr(l,n+1,respant);//primeiro r tal que resposta de r seja <= respant
    //cout<<"e achamos o r = "<<r<<endl;
    ll aux=pegasoma(l,r-1)+(n-r+1LL)*respant;
    //cout<<"pr esse L, a soma eh "<<aux<<endl;
    resp+=pegasoma(l,r-1);
    resp+=(n-r+1LL)*respant;
  }

}

int main()
{
  memset(seg,INF,sizeof(seg));
  memset(pri,INF,sizeof(pri));
  cin>>n;
  for(int i=1;i<=n;i++)
  {
    cin>>a[i];
    pen[a[i]]=ult[a[i]];
    ult[a[i]]=i;
    if(pri[a[i]]==INF)
    {
      pri[a[i]]=i;
    }
    else if(seg[a[i]]==INF)
    {
      seg[a[i]]=i;
    }
  }
  sieve();
  solve();
  //for(int i=1;i<=20;i++) cout<<"val = "<<i<<", pri = "<<pri[i]<<" , seg = "<<seg[i]<<", pen = "<<pen[i]<<", ult = "<<ult[i]<<endl;
  cout<<resp<<endl;
  return 0;
}