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

#include <bits/stdc++.h>

using namespace std;
#define inf 0x3f3f3f3f
#define INF 2e18
#define eps 1e-9

#define FastRead                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
#define fRead freopen("input.txt", "r", stdin)
#define fWrite freopen("output.txt", "w", stdout)

#define LL long long
#define ull unsigned long long
#define PI acos(-1.0)
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pLL pair<LL, LL>
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define SQR(a) ((a) * (a))
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define max5(a, b, c, d, e) max(max3(a, b, c), max(d, e))
#define min5(a, b, c, d, e) min(min3(a, b, c), min(d, e))
#define vi vector<int>
#define vL vector<LL>
#define LB(a, x)                                                               \\
  (lower_bound(all(a), x) -                                                    \\
   a.begin()) //  first element in the range [first,last) which does not compare
              //  less than val.
#define UB(a, x)                                                               \\
  (upper_bound(all(a), x) -                                                    \\
   a.begin()) //  first element in the range [first,last) which compares greater
              //  than val.
#define prec(n) fixed << setprecision(n)

#define MEM(a, x) memset(a, x, sizeof(a))
#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())
#define Unique(a) sort(all(a)), a.erase(unique(all(a)), a.end())

#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, b) for (int i = 0; i < b; i++)
#define IT(it, x) for (it = x.begin(); it != x.end(); it++)
const LL N = 10000009;
LL s[N + 5];

int main() {
  LL n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j += i)
      s[j]++;
  }
  LL ans = 0;
  for (LL i = 1; i <= n; i++)
    ans += (i * s[i]);
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define inf 0x3f3f3f3f
#define INF 2e18
#define eps 1e-9

#define FastRead                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
#define fRead freopen("input.txt", "r", stdin)
#define fWrite freopen("output.txt", "w", stdout)

#define LL long long
#define ull unsigned long long
#define PI acos(-1.0)
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pLL pair<LL, LL>
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define SQR(a) ((a) * (a))
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define max5(a, b, c, d, e) max(max3(a, b, c), max(d, e))
#define min5(a, b, c, d, e) min(min3(a, b, c), min(d, e))
#define vi vector<int>
#define vL vector<LL>
#define LB(a, x)                                                               \\
  (lower_bound(all(a), x) -                                                    \\
   a.begin()) //  first element in the range [first,last) which does not compare
              //  less than val.
#define UB(a, x)                                                               \\
  (upper_bound(all(a), x) -                                                    \\
   a.begin()) //  first element in the range [first,last) which compares greater
              //  than val.
#define prec(n) fixed << setprecision(n)

#define MEM(a, x) memset(a, x, sizeof(a))
#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())
#define Unique(a) sort(all(a)), a.erase(unique(all(a)), a.end())

#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, b) for (int i = 0; i < b; i++)
#define IT(it, x) for (it = x.begin(); it != x.end(); it++)
const LL N = 10000009;
LL a[N + 5];
LL s[N + 5];

#define n 10000009
int ara[n];
vector<LL> prime;
void sieve() {
  ara[0] = ara[1] = 1;
  for (LL i = 2; i < n; i++) {
    if (ara[i] == 0) {
      for (LL j = i * 2; j < n; j += i)
        ara[j] = 1;
    }
  }
  for (LL i = 0; i < n; i++) {
    if (ara[i] == 0)
      prime.push_back(i);
  }
}
LL divisor(long long num) {

  LL ans = 1, cnt = 0;
  for (LL i = 0; prime[i] * prime[i] <= num; i++) {
    if (num % prime[i] == 0) {
      cnt = 0;
      while (num % prime[i] == 0) {
        num /= prime[i];
        cnt++;
      }
      ans *= (cnt + 1);
    }
  }
  if (num > 1)
    ans = ans * 2;
  return ans;
}
int main() {
  FastRead;
  sieve();
  //    cout<<prime.size()<<endl;
  for (LL i = 1; i <= N; i++) {

    a[i] = divisor(i);
  }
  //    for(int i=1;i<=10;i++)
  //        cout<<a[i]<<" ";
  //    cout<<endl;
  for (LL i = 1; i <= N; i++) {
    s[i] = a[i] * i;
    s[i] += s[i - 1];
  }
  LL g;
  cin >> g;
  cout << s[g];
}