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

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define repi(i, x)                                                             \\
  for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a) ? "Yes" : "No")
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int, int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INFLL = 1LL << 60;
const int INF = 1 << 30;
const double PI = acos(-1);
int main() {
  int n;
  ll ans = 0LL;
  cin >> n;
  vector<ll> dis(n + 1, 0LL);
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j += i) {
      dis[j]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    ans += dis[i] * i;
  }
  cout << ans << endl;
}
/*
2  1 2 =>2   2
3  1 3 ->2        3
4  1 2 4 => 3    2^2
6 1 2 3 6 -> 4            2 3
12 => 1 2 3 4 6  12 => 6  2^2 3
2 3 5 => 8 (30)
2 3 => 4 (6)

1 1   1
2 4 (1)   5
3 6    11
4 12  (2) 23
5 10  33
6 24   57
7 14   71
8 32 (4)  103


16 60 (5)

2 ^1 2 * 2^1 = n*2

2^2  3 * 2^2 -> 3*2n

2^3  4 * 2-3 4* 2^2 n

2^4  5 * 2^4 5*2^3 n

2 3 => 4 * n*3

*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define repi(i, x)                                                             \\
  for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a) ? "Yes" : "No")
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int, int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INFLL = 1LL << 60;
const int INF = 1 << 30;
const double PI = acos(-1);
int main() {
  int n;
  ll ans = 1;
  cin >> n;
  vector<map<int, int>> dis(n + 1);
  dis[1][1] = 1;
  for (int i = 2; i <= n; i++) {
    if (dis[i].size() == 0 or dis[i].size() == 1) {
      if (dis[i].size() == 0) {
        dis[i][i] = 0;
      }

      for (int s = i; s <= n; s += i) {
        dis[s][dis[i].begin()->F] += 0;
        dis[s][dis[i].begin()->F]++;
      }
    }
  }
  ll c = 1LL;
  for (int i = 2; i <= n; i++) {
    c = 1LL;
    repi(itr, dis[i]) {
      c *= (1 + (itr->S));
      // clog<<(itr->F)<<" ";
    }
    ans += c * i;
    //	clog<<"! "<<c<<" "<<i<<endl;
  }
  cout << ans << endl;
}
/*
2  1 2 =>2   2
3  1 3 ->2        3
4  1 2 4 => 3    2^2
6 1 2 3 6 -> 4            2 3
12 => 1 2 3 4 6  12 => 6  2^2 3
2 3 5 => 8 (30)
2 3 => 4 (6)

1 1   1
2 4 (1)   5
3 6    11
4 12  (2) 23
5 10  33
6 24   57
7 14   71
8 32 (4)  103


16 60 (5)

2 ^1 2 * 2^1 = n*2

2^2  3 * 2^2 -> 3*2n

2^3  4 * 2-3 4* 2^2 n

2^4  5 * 2^4 5*2^3 n

2 3 => 4 * n*3

*/