Problem p02624 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
  || Shree Ganeshay Namah ||

     || Jai Shree Ram ||
  || Nabh Sparsham Diptam ||
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mod 1000000007
#define block 180
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define f1 first
#define s1 second
int jai_shree_ram() { return 1; }
int shree_ganeshay_namah() { return 1; }
ll power(ll x, ll y) {
  ll r = 1;
  while (y > 0) {
    if (y % 2 != 0)
      r = r * x;
    x = x * x;
    y = y / 2;
  }
  return r;
}
ll ncr(ll n, ll k) {

  ll c[k + 1];
  memset(c, 0, sizeof(c));
  c[0] = 1;
  for (ll i = 1; i <= n; i++) {
    for (ll j = min(i, k); j > 0; j--)
      c[j] = c[j] + c[j - 1];
  }
  return c[k];
}

bool prime[500001];
void sieve() {
  ll i, j;
  memset(prime, true, sizeof(prime));
  prime[0] = false;
  prime[1] = false;
  for (i = 2; i <= sqrt(100001); i++) {
    if (prime[i]) {
      for (j = i * i; j <= 100001; j = j + i)
        prime[j] = false;
    }
  }
}

/*
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n,m,i,j,k;
    cin>>n>>m>>k;
    ll a[n],b[m];
    for(i=0;i<n;i++)
        cin>>a[i];
    for(i=0;i<m;i++)
        cin>>b[i];
    ll f=0;
    i=0,j=0;
    ll z=0;
    while(1)
    {
        if(i==n && j==m)
            break;
        if(i<n && j<m)
        {
            if(a[i]<=b[j])
            {
                if(f+a[i]>k)
                    break;
                f=f+a[i];
                i++;
                z++;
            }
            else
            {
                if(f+b[j]>k)
                    break;
                f=f+b[j];
                j++;
                z++;
            }
        }
        else if(i<n && j>=m)
        {
            if(f+a[i]>k)
                break;
            else
            {
                f=f+a[i];
                i++;
                z++;
            }
        }
        else
        {
            if(f+b[j]>k)
                break;
            else
            {
                f=f+b[j];
                j++;
                z++;
            }
        }
    }
    cout<<z;
    return 0;

}
*/
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, i, j;
  cin >> n;
  ll f = 0;
  for (i = 1; i <= sqrt(n); i++) {

    f = f + i * (i + n / i) * (n / i + 1 - i);
  }
  ll k = sqrt(n);
  f = f - k * (k + 1) * (2 * k + 1) / 6;
  cout << f;
  return 0;
}


Error: TLE

Fixed submission:

/*
  || Shree Ganeshay Namah ||

     || Jai Shree Ram ||
  || Nabh Sparsham Diptam ||
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mod 1000000007
#define block 180
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define f1 first
#define s1 second
int jai_shree_ram() { return 1; }
int shree_ganeshay_namah() { return 1; }
ll power(ll x, ll y) {
  ll r = 1;
  while (y > 0) {
    if (y % 2 != 0)
      r = r * x;
    x = x * x;
    y = y / 2;
  }
  return r;
}
ll ncr(ll n, ll k) {

  ll c[k + 1];
  memset(c, 0, sizeof(c));
  c[0] = 1;
  for (ll i = 1; i <= n; i++) {
    for (ll j = min(i, k); j > 0; j--)
      c[j] = c[j] + c[j - 1];
  }
  return c[k];
}

bool prime[500001];
void sieve() {
  ll i, j;
  memset(prime, true, sizeof(prime));
  prime[0] = false;
  prime[1] = false;
  for (i = 2; i <= sqrt(100001); i++) {
    if (prime[i]) {
      for (j = i * i; j <= 100001; j = j + i)
        prime[j] = false;
    }
  }
}

/*
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n,m,i,j,k;
    cin>>n>>m>>k;
    ll a[n],b[m];
    for(i=0;i<n;i++)
        cin>>a[i];
    for(i=0;i<m;i++)
        cin>>b[i];
    ll f=0;
    i=0,j=0;
    ll z=0;
    while(1)
    {
        if(i==n && j==m)
            break;
        if(i<n && j<m)
        {
            if(a[i]<=b[j])
            {
                if(f+a[i]>k)
                    break;
                f=f+a[i];
                i++;
                z++;
            }
            else
            {
                if(f+b[j]>k)
                    break;
                f=f+b[j];
                j++;
                z++;
            }
        }
        else if(i<n && j>=m)
        {
            if(f+a[i]>k)
                break;
            else
            {
                f=f+a[i];
                i++;
                z++;
            }
        }
        else
        {
            if(f+b[j]>k)
                break;
            else
            {
                f=f+b[j];
                j++;
                z++;
            }
        }
    }
    cout<<z;
    return 0;

}
*/
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, i, j;
  cin >> n;
  ll f = 0;
  for (i = 1; i <= n; i++) {
    set<ll> s;
    for (j = 1; j <= sqrt(i); j++) {
      if (i % j == 0) {
        s.insert(j);
        ll z = i / j;
        if (i % z == 0)
          s.insert(z);
      }
    }
    f = f + i * (s.size());
  }
  cout << f;
  return 0;
}