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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double lld;
ll MOD = (1e9) + 7;
const int dr[] = {+1, -1, +0, +0, +1, -1, +1, -1};
const int dc[] = {+0, +0, +1, -1, +1, -1, -1, +1};
const int kx[] = {+1, +2, -1, -2, +1, +2, -1, -2};
const int ky[] = {+2, +1, +2, +1, -2, -1, -2, -1};
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
inline void fastIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  string sep;
  for (const auto &x : v)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <typename T, size_t size>
ostream &operator<<(ostream &os, const array<T, size> &arr) {
  os << '{';
  string sep;
  for (const auto &x : arr)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << ' ' << H;
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#else
#define debug(...) 47
#endif

int main() {
  fastIO();
  ll ans = 0;
  ll n, c = 100;
  vector<ll> factors(10101010);
  cin >> n;
  for (ll i = 1; i <= 1e7 + c; i++) {
    for (ll j = i; j <= 1e7 + c; j += i) {
      factors[j]++;
    }
  }
  for (ll i = 1; i <= n; i++) {
    ans += i * factors[i];
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double lld;
ll MOD = (1e9) + 7;
const int dr[] = {+1, -1, +0, +0, +1, -1, +1, -1};
const int dc[] = {+0, +0, +1, -1, +1, -1, -1, +1};
const int kx[] = {+1, +2, -1, -2, +1, +2, -1, -2};
const int ky[] = {+2, +1, +2, +1, -2, -1, -2, -1};
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
inline void fastIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  string sep;
  for (const auto &x : v)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <typename T, size_t size>
ostream &operator<<(ostream &os, const array<T, size> &arr) {
  os << '{';
  string sep;
  for (const auto &x : arr)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << ' ' << H;
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#else
#define debug(...) 47
#endif
const int MAXN = 10000010;
vector<vector<int>> factors(MAXN);
int main() {
  fastIO();
  ll ans = 0;
  int n, c = 10;
  cin >> n;
  vector<bool> vis(n + c, false);
  factors[1].push_back(1);
  for (int i = 2; i <= n; i++) {
    factors[i].push_back(1);
    if (!vis[i]) {
      for (int j = i; j <= n; j += i) {
        vis[j] = true;
        int temp = j;
        while (temp % i == 0) {
          factors[j].push_back(i), temp /= i;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    ll nFactors = 1, cnt = 1;
    for (int j = 0; j < factors[i].size(); j++) {
      if (j > 0 && factors[i][j] == factors[i][j - 1])
        cnt++;
      if (j > 0 && factors[i][j] != factors[i][j - 1]) {
        nFactors *= (cnt + 1), cnt = 1;
      }
    }
    nFactors *= (cnt + 1);
    ans += nFactors * i;
  }
  cout << ans / 2 << endl;
  return 0;
}