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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll M = 1e9 + 7;
const int iinf = 1 << 29;
const long long llinf = 1ll << 60;
const double PI = 3.14159265;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll mrand(ll B) { return (ull)rng() % B; }

void work() {
  int n;
  cin >> n;
  ll ans = 0;
  for (ll i = 1; i <= n; ++i) {
    ll f = n / i;
    ans += i * ((f * f + f) >> 1);
  }
  cout << ans << endl;
  /*
  int n; cin>>n;
  vector<vector<int>> s(n+1, vector<int>(50));
  vector<int> p(n+1, 0);
  for (ll i = 2; i <= n; ++i) {
      if (p[i] == 0) {
          s[i][p[i]++] = i;
          for (int j = i + i; j <= n; j += i)
              s[j][p[j]++] = i;
      }
  }

  ll ans = 1;
  for (ll i = 2; i <= n; ++i) {
      ll cur = 1;
      ll c = i;
      for (int j = 0; j < p[i]; ++j) {
          int nd = 0;
          while (c%s[i][j] == 0) {
              ++nd;
              c /= s[i][j];
          }
          cur *= nd + 1;
      }
      ans += i * cur;
  }
  cout << ans << endl;
  */
  /*
  int n; cin>>n;
  vector<int> a(n);
  for (auto& e : a) cin>>e;
  vector<int> b(n);
  for (auto& e : b) cin>>e;
  priority_queue<pair<ll, int>> c;
  for (int i = 0; i < n; ++i) {
      c.push({a[i], i});
  }

  sort(b.begin(), b.end());
  reverse(b.begin(), b.end());
  vector<int> ans(n, -1);
  for (auto e : b) {
      pair<ll, int> t = c.top();
      int prev = t.second == 0 ? n - 1 : i - 1;
      while (ans[t.second] != -1 && ans[prev] != -1) {
          c.pop();
          t = c.top();
          prev = t.second == 0 ? n - 1 : i - 1;
      }
      c.pop();
      c.push({t.first - e, t.second})
      if (ans[t.second] == -1)
  }
  */
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // int nt; cin>>nt; while (nt--) work();
  work();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll M = 1e9 + 7;
const int iinf = 1 << 29;
const long long llinf = 1ll << 60;
const double PI = 3.14159265;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll mrand(ll B) { return (ull)rng() % B; }

void work() {
  int n;
  cin >> n;
  vector<vector<int>> s(n + 1, vector<int>(50));
  vector<int> p(n + 1, 0);
  for (ll i = 2; i <= n; ++i) {
    if (p[i] == 0) {
      s[i][p[i]++] = i;
      for (int j = i + i; j <= n; j += i)
        s[j][p[j]++] = i;
    }
  }
  // cerr << "here\\n" << endl;
  ll ans = 1;
  for (ll i = 2; i <= n; ++i) {
    ll cur = 1;
    ll c = i;
    for (int j = 0; j < p[i]; ++j) {
      int nd = 0;
      while (c % s[i][j] == 0) {
        ++nd;
        c /= s[i][j];
      }
      cur *= nd + 1;
    }
    ans += i * cur;
  }
  cout << ans << endl;
  /*
  int n; cin>>n;
  vector<int> a(n);
  for (auto& e : a) cin>>e;
  vector<int> b(n);
  for (auto& e : b) cin>>e;
  priority_queue<pair<ll, int>> c;
  for (int i = 0; i < n; ++i) {
      c.push({a[i], i});
  }

  sort(b.begin(), b.end());
  reverse(b.begin(), b.end());
  vector<int> ans(n, -1);
  for (auto e : b) {
      pair<ll, int> t = c.top();
      int prev = t.second == 0 ? n - 1 : i - 1;
      while (ans[t.second] != -1 && ans[prev] != -1) {
          c.pop();
          t = c.top();
          prev = t.second == 0 ? n - 1 : i - 1;
      }
      c.pop();
      c.push({t.first - e, t.second})
      if (ans[t.second] == -1)
  }
  */
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // int nt; cin>>nt; while (nt--) work();
  work();
  return 0;
}