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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

#define mod (int)1e9 + 7
#define MOD 998244353
#define ll long long
#define mp make_pair
#define F first
#define S second
#define t()                                                                    \\
  int test;                                                                    \\
  cin >> test;                                                                 \\
  while (test--)
#define ii pair<int, int>
#define que_max priority_queue<int>
#define que_min priority_queue<int, vector<int>, greater<int>>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
  // use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

bool comparator(ii a, ii b) {
  if (a.F == b.F) {
    return (a.S < b.S);
  } else {
    return (a.F > b.F);
  }
}

int32_t main()

{
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif

  IOS;

  int n;
  cin >> n;

  ll ans = 0;

  for (int i = 1; i <= n; i++) {
    ll int y = n / i;

    ans += i * (y * (y + 1)) / 2;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

#define mod (int)1e9 + 7
#define MOD 998244353
#define ll long long
#define mp make_pair
#define F first
#define S second
#define t()                                                                    \\
  int test;                                                                    \\
  cin >> test;                                                                 \\
  while (test--)
#define ii pair<int, int>
#define que_max priority_queue<int>
#define que_min priority_queue<int, vector<int>, greater<int>>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
  // use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

bool comparator(ii a, ii b) {
  if (a.F == b.F) {
    return (a.S < b.S);
  } else {
    return (a.F > b.F);
  }
}

int mx = 100000;

vector<bool> isprime(mx + 5, true);

vector<ll int> primes;

void sieve() {
  ll int i, j;
  isprime[0] = false;
  isprime[1] = false;

  for (i = 2; i <= mx; i++) {
    if (isprime[i]) {
      primes.push_back(i);

      for (j = i * i; j <= mx; j += i) {
        isprime[j] = false;
      }
    }
  }
}

ll numDiv(ll n) {
  ll pf_idx = 0, pf = primes[pf_idx], ans = 1;
  while (pf * pf <= n) {
    ll power = 0;
    while (n % pf == 0) {
      n /= pf;
      power++;
    }
    ans *= (power + 1);
    pf = primes[++pf_idx];
  }
  if (n != 1)
    ans *= 2;
  return ans;
}
int32_t main()

{
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif

  IOS;

  sieve();

  int n;
  cin >> n;
  ll ans = 0;

  for (int i = 1; i <= n; i++) {
    ans += i * numDiv(i);
  }

  cout << ans << endl;
}