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>
#define leading                                                                \\
  zero str.erase(0, min(str.find_first_not_of('0'), str.size() - 1));
using namespace __gnu_pbds;
using namespace std;
typedef unsigned long long ll;
typedef pair<int, int> pii;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
string text = "abcdefghijklmnopqrstuvwxyz";
const int maxn = 1e6 + 7;
//        .--------------.
//        | Try First One|
//        '--------------'
//                |     .--------------.
//                |     |              |
//                V     V              |
//              .--------------.       |
//              |      AC.     |<---.  |
//              '--------------'    |  |
//              (True)|  |(False)   |  |
//           .--------'  |          |  |
//           |           V          |  |
//           |  .--------------.    |  |
//           |  |   Try Again  |----'  |
//           |  '--------------'       |
//           |                         |
//           |  .--------------.       |
//           '->| Try Next One |-------'
//              '--------------'

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  ll div[n + 2] = {0};
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j += i)
      div[j]++;
  }
  for (int i = 1; i <= n; i++)
    sum += (i * div[i]);
  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define leading                                                                \\
  zero str.erase(0, min(str.find_first_not_of('0'), str.size() - 1));
using namespace __gnu_pbds;
using namespace std;
typedef unsigned long long ll;
typedef pair<int, int> pii;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
string text = "abcdefghijklmnopqrstuvwxyz";
const int maxn = 1e6 + 7;
//        .--------------.
//        | Try First One|
//        '--------------'
//                |     .--------------.
//                |     |              |
//                V     V              |
//              .--------------.       |
//              |      AC.     |<---.  |
//              '--------------'    |  |
//              (True)|  |(False)   |  |
//           .--------'  |          |  |
//           |           V          |  |
//           |  .--------------.    |  |
//           |  |   Try Again  |----'  |
//           |  '--------------'       |
//           |                         |
//           |  .--------------.       |
//           '->| Try Next One |-------'
//              '--------------'

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  vector<pair<ll, ll>> v;
  for (ll i = 2; i <= n; i++) {
    ll x = i;
    ll tot = 1;
    for (ll j = 2; j * j <= x; j++) {
      if (x % j == 0) {
        int cnt = 0;
        while (x % j == 0) {
          cnt++;
          x /= j;
        }
        tot *= (cnt + 1);
      }
    }
    if (x > 1)
      tot *= 2;
    v.push_back({i, tot});
  }
  ll sum = 1;
  for (auto x : v) {
    sum += (x.first * x.second);
  }
  cout << sum << endl;
}