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

#include <bits/stdc++.h>
using namespace std;

#define int long long int
#define ff first
#define ss second
#define pb push_back

#define vi vector<int>
#define pii pair<int, int>

#define loop(i, s, e) for (int i = s; i < e; i++)
#define rloop(i, e, s) for (int i = e; i >= s; i--)
#define mset(a, f) memset(a, f, sizeof(a))

#define endl "\\n"
#define sz(x) ((int)x.size())
#define all(p) p.begin(), p.end()

#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define print(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl
#define Print(a, x, y)                                                         \\
  for (int i = x; i < y; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << endl

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

const int N = 200005;

void solve() {
  int n;
  cin >> n;
  int ans = 0;
  vi cnt(n + 1, 0);

  loop(i, 1, n + 1) {
    for (int j = i; j <= n; j += i)
      cnt[j]++;
  }

  loop(i, 1, n + 1) { ans += cnt[i] * i; }
  cout << ans;
  return;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int t = 1;
  // cin >> t;
  while (t--)
    solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long int
#define ff first
#define ss second
#define pb push_back

#define vi vector<int>
#define pii pair<int, int>

#define loop(i, s, e) for (int i = s; i < e; i++)
#define rloop(i, e, s) for (int i = e; i >= s; i--)
#define mset(a, f) memset(a, f, sizeof(a))

#define endl "\\n"
#define sz(x) ((int)x.size())
#define all(p) p.begin(), p.end()

#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define print(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl
#define Print(a, x, y)                                                         \\
  for (int i = x; i < y; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << endl

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

const int N = 200005;

int go(int n) {
  int ans = 1;
  if (n == 1)
    return 1;

  for (int i = 2; i * i <= n; i++) {
    int cnt = 1;

    while (n > 1 and n % i == 0) {
      cnt++;
      n /= i;
    }
    ans *= cnt;
  }
  if (n != 1)
    ans *= 2;

  return ans;
}

void solve() {
  int n;
  cin >> n;
  int ans = 0;

  loop(i, 1, n + 1) {
    int x = go(i);
    int z = i * x;
    ans = ans + z;
  }
  cout << ans;
  return;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int t = 1;
  // cin >> t;
  while (t--)
    solve();

  return 0;
}