Problem p02624 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define ll long long
#define int long long
#define inf ((ll)1e18)
#define double long double
#define ull unsigned long long
#define mod 1000000007
#define vi vector<ll>
#define extreme inline
#define ppi pair<ll, pair<ll, ll>>
#define pb push_back
#define mp make_pair
#define pi 2 * acos(0.0)
bool cust(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}
#define pr(a, x, y)                                                            \\
  for (int i = x; i < y; i++) {                                                \\
    cout << a[i] << " ";                                                       \\
  }
#define p(a) cout << a;
#define sp(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  ll x;                                                                        \\
  cin >> x;                                                                    \\
  while (x--)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define test cout << "THIS_AND_THIS" << endl;
#define str string
void kehsihba() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  /*#ifndef ONLINE_JUDGE
          freopen("input.txt", "r", stdin);
          freopen("output.txt", "w", stdout);
  #endif*/
}
int dive[10000001];
void ans(int n) {
  memset(dive, 0, n + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j * i <= n; j++)
      dive[i * j]++;
  }
  for (int i = 1; i <= n; i++) {
    dive[i] *= i;
    if (i != 1) {
      dive[i] += dive[i - 1];
    }
  }
}
int32_t main() {
  kehsihba();
  ll n;
  cin >> n;
  ans(n);
  cout << dive[n];
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define ll long long
#define int long long
#define inf ((ll)1e18)
#define double long double
#define ull unsigned long long
#define mod 1000000007
#define vi vector<ll>
#define extreme inline
#define ppi pair<ll, pair<ll, ll>>
#define pb push_back
#define mp make_pair
#define pi 2 * acos(0.0)
bool cust(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}
#define pr(a, x, y)                                                            \\
  for (int i = x; i < y; i++) {                                                \\
    cout << a[i] << " ";                                                       \\
  }
#define p(a) cout << a;
#define sp(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  ll x;                                                                        \\
  cin >> x;                                                                    \\
  while (x--)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define test cout << "THIS_AND_THIS" << endl;
#define str string
void kehsihba() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  /*#ifndef ONLINE_JUDGE
          freopen("input.txt", "r", stdin);
          freopen("output.txt", "w", stdout);
  #endif*/
}
int dive[10000001];
void ans() {
  memset(dive, 0, sizeof(dive));
  for (int i = 1; i <= 10000001; i++) {
    for (int j = 1; j * i <= 10000001; j++)
      dive[i * j]++;
  }
  for (int i = 1; i <= 10000001; i++) {
    dive[i] *= i;
    if (i != 1) {
      dive[i] += dive[i - 1];
    }
  }
}
int32_t main() {
  ll n;
  cin >> n;
  ans();
  cout << dive[n];
}