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

#include <bits/stdc++.h>
#define ll long long
#define endl "\\n"

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define mi map<int, int>
#define pi pair<int, int>

#define Fori(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)

#define pb push_back
#define mp make_pair

#define read(a, n)                                                             \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];
#define print(x) cout << "X: == " << x << endl;
#define printMap(a)                                                            \\
  for (auto it = a.begin(); it != a.end(); it++) {                             \\
    cout << it->first << "  " << it->second << endl;                           \\
  }
#define printVector(a)                                                         \\
  for (auto it = a.begin(); it != a.end(); it++) {                             \\
    cout << *it << endl;                                                       \\
  }

using namespace std;
int main() {
  IOS ll n;
  cin >> n;
  vl a(n + 1);
  Fori(i, 1, n + 1) {
    for (int j = i; j <= n; j += i) {
      a[j]++;
    }
  }
  ll ans = 0;
  Fori(i, 1, n + 1) { ans += i * a[i]; }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define endl "\\n"

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define mi map<int, int>
#define pi pair<int, int>

#define Fori(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i >= b; i--)

#define pb push_back
#define mp make_pair

#define read(a, n)                                                             \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];
#define print(x) cout << "X: == " << x << endl;
#define printMap(a)                                                            \\
  for (auto it = a.begin(); it != a.end(); it++) {                             \\
    cout << it->first << "  " << it->second << endl;                           \\
  }
#define printVector(a)                                                         \\
  for (auto it = a.begin(); it != a.end(); it++) {                             \\
    cout << *it << endl;                                                       \\
  }

using namespace std;
int main() {
  IOS int n;
  cin >> n;
  vl a(n + 1);
  vector<bool> primes(10000, true);
  Fori(i, 2, 10000) {
    int sqr_root = sqrt(i);
    Fori(j, 2, sqr_root + 1) {
      if (i % j == 0) {
        primes[i] = false;
        break;
      }
    }
  }
  a[1] = 1;
  Fori(i, 2, n + 1) {
    int pt = i, total = 1;
    Fori(j, 2, i + 1) {
      if (primes[j]) {
        int cnt = 0;
        if (pt % j == 0) {
          while (pt % j == 0) {
            pt /= j;
            cnt++;
          }
        }
        total *= (cnt + 1);
      }
    }
    a[i] = total;
  }
  ll ans = 0;
  Fori(i, 1, n + 1) { ans += i * a[i]; }
  cout << ans << endl;
  return 0;
}