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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ub upper_bound
#define lb lower_bound
#define ins insert
#define int long long
#define endl '\\n'
#define ff first
#define ss second
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define si set<int>
#define usi unordered_set<int>
#define mii map<int, int>
#define umii unordered_map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define repd(i, a, n) for (int i = a; i > n; --i)
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define Mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define deb(x) cout << #x << "=" << x << endl;
#define deb2(x, y) cout << #x << "=" << x << "  " << #y << "=" << y << endl;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
int Pow(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res *= a;
    a *= a;
    b /= 2;
  }
  return res;
}
int MPow(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % Mod;
    a = (a * a) % Mod;
    b /= 2;
  }
  return res % Mod;
}
int log(int x) { return 64 - __builtin_clzll(x) - 1; }

void c_p_p() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}
struct Node {
  int a, b, c, d;
};
bool cmp(Node A, Node B) { return A.a < B.a; }
const int Max = 1e7 + 1;
int A[Max];

int32_t main() {
  c_p_p();
  int n;
  cin >> n;

  rep(i, 2, n + 1) {
    for (int j = i; j <= n; j += i)
      A[j] += 1;
  }
  int ans = 0;
  rep(i, 1, n + 1) {
    ans += (A[i] + 1) * i;
    // deb(A[i]);
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ub upper_bound
#define lb lower_bound
#define ins insert
#define int long long
#define endl '\\n'
#define ff first
#define ss second
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define si set<int>
#define usi unordered_set<int>
#define mii map<int, int>
#define umii unordered_map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define repd(i, a, n) for (int i = a; i > n; --i)
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define Mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define deb(x) cout << #x << "=" << x << endl;
#define deb2(x, y) cout << #x << "=" << x << "  " << #y << "=" << y << endl;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
int Pow(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res *= a;
    a *= a;
    b /= 2;
  }
  return res;
}
int MPow(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % Mod;
    a = (a * a) % Mod;
    b /= 2;
  }
  return res % Mod;
}
int log(int x) { return 64 - __builtin_clzll(x) - 1; }

void c_p_p() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}
struct Node {
  int a, b, c, d;
};
bool cmp(Node A, Node B) { return A.a < B.a; }
const int Max = 1e7;
int Fac[Max + 1];
void Sieve(int n) {
  for (int i = 2; i <= sqrt(n); ++i) {
    if (Fac[i] == 0) {
      for (int j = i * i; j <= n; j += i)
        Fac[j] = 1;
    }
  }
}

int32_t main() {
  c_p_p();
  int n;
  cin >> n;
  Sieve(n);
  int ans = 1;
  rep(i, 2, n + 1) {
    if (Fac[i] == 0)
      ans += i * 2;
    else {
      int cnt = 0;
      for (int j = 1; j <= sqrt(i); ++j) {
        if (i % j == 0) {
          if (i / j != j)
            cnt += 2;
          else
            ++cnt;
        }
      }
      // deb2(i,cnt);
      ans += cnt * i;
    }
  }
  cout << ans;
  return 0;
}