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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
typedef long double ld;
#define pb push_back
#define f(i, a, b) for (int i = a; i < b; i++)
#define fd(i, a, b) for (int i = a - 1; i >= b; i--)
#define pf push_front
#define fi first
#define se second
#define INF 1e10
const double pi = 3.14159265358979323;
const int mod = 998244353;
const int MAX = 1e7 + 5;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
void swap(int a, int b) {
  int temp = a;
  a = b;
  b = temp;
}
int binpow(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
int modinv(int y) { return binpow(y, mod - 2); }

int fact[500000 + 5];
void precal() {
  fact[0] = 1;
  for (int i = 1; i < 500000 + 5; i++) {
    fact[i] = (i * fact[i - 1]) % mod;
  }
}
bool isPrime(int n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}
int ans[MAX] = {0};
;
signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    freopen("OUTPUT.txt", "w", stdout);
  }
#endif
  // -------------------------------------Code starts
  // here---------------------------------------------------------------------
  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    f(i, 1, n + 1) {
      for (int j = 1; j * i <= n; j++) {
        ans[i * j]++;
      }
    }
    int tot = 0;
    f(i, 1, n + 1) { tot += (ans[i] * i); }
    cout << tot << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
typedef long double ld;
#define pb push_back
#define f(i, a, b) for (int i = a; i < b; i++)
#define fd(i, a, b) for (int i = a - 1; i >= b; i--)
#define pf push_front
#define fi first
#define se second
#define INF 1e10
const double pi = 3.14159265358979323;
const int mod = 998244353;
const int MAX = 1e7 + 5;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
void swap(int a, int b) {
  int temp = a;
  a = b;
  b = temp;
}
int binpow(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
int modinv(int y) { return binpow(y, mod - 2); }

int fact[500000 + 5];
void precal() {
  fact[0] = 1;
  for (int i = 1; i < 500000 + 5; i++) {
    fact[i] = (i * fact[i - 1]) % mod;
  }
}
bool isPrime(int n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}
bool prime[10000000 + 1];

int dp[10000000 + 1] = {};
signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    freopen("OUTPUT.txt", "w", stdout);
  }
#endif
  // -------------------------------------Code starts
  // here---------------------------------------------------------------------
  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    memset(prime, true, sizeof(prime));
    for (int p = 2; p * p <= n; p++) {
      // If prime[p] is not changed, then it is a prime
      if (prime[p] == true) {
        // Update all multiples of p greater than or
        // equal to the square of it
        // numbers which are multiple of p and are
        // less than p^2 are already been marked.
        for (int i = p * p; i <= n; i += p)
          prime[i] = false;
      }
    }
    int ans = 1;
    dp[1] = 1;
    f(i, 2, n + 1) {
      int res = 1;
      int nm = i;
      if (prime[nm]) {
        ans += (nm * 2);
        dp[i] = 2;
      } else {
        int x = nm;
        for (int j = 2; j <= sqrt(nm); j++) {
          if (nm % j == 0) {
            x = j;
            break;
          }
        }
        int ct = 0;
        while (nm % x == 0) {
          ct++;
          nm /= x;
        }
        dp[i] = ct + 1;
        int as = dp[i / x];
        dp[i] *= (as / ct);
        ans = ans + (dp[i] * i);
      }
    }
    cout << ans << endl;
  }
}