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

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define pb push_back
#define mk make_pair
#define MAX 1000001
ll prime[MAX];
#define mod 1000000007

ll rec(vector<vector<ll>> &dp, vector<ll> &arr, int n, int i, int k, int w) {
  ll ans = 0;
  if (i >= n)
    return 0;

  if (dp[i][k] != -1)
    return dp[i][k];

  if (k != 0)
    ans = max(rec(dp, arr, n, i + 1, w, w),
              arr[i] + rec(dp, arr, n, i + 1, k - 1, w));
  else
    ans = max(rec(dp, arr, n, i + 1, k, w), rec(dp, arr, n, i + 1, w, w));

  return dp[i][k] = ans;
}

ll t[400008];
void build(vector<ll> &a, int v, int tl, int tr) {
  if (tl == tr) {
    t[v] = a[tl];
  } else {
    int tm = (tl + tr) / 2;
    build(a, v * 2, tl, tm);
    build(a, v * 2 + 1, tm + 1, tr);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}

ll maxi(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    return t[v];
  }
  int tm = (tl + tr) / 2;
  return max(maxi(v * 2, tl, tm, l, min(r, tm)),
             maxi(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}

void update(int v, int tl, int tr, int pos, ll new_val) {
  if (tl == tr) {
    t[v] = new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2, tl, tm, pos, new_val);
    else
      update(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}

void sieve(ll divi[], ll n) {}

int main() {

  ll n;
  cin >> n;
  ll divi[n + 1] = {0};

  for (int i = 1; i <= n; i++) {
    divi[i]++;
    for (int j = i * 2; j <= n; j += i) {
      divi[j]++;
    }
  }

  ll ans = 0;
  for (int i = 1; i <= n; i++)
    ans += i * divi[i];

  cout << ans;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define pb push_back
#define mk make_pair
#define MAX 1000001
ll prime[MAX];
#define mod 1000000007

ll rec(vector<vector<ll>> &dp, vector<ll> &arr, int n, int i, int k, int w) {
  ll ans = 0;
  if (i >= n)
    return 0;

  if (dp[i][k] != -1)
    return dp[i][k];

  if (k != 0)
    ans = max(rec(dp, arr, n, i + 1, w, w),
              arr[i] + rec(dp, arr, n, i + 1, k - 1, w));
  else
    ans = max(rec(dp, arr, n, i + 1, k, w), rec(dp, arr, n, i + 1, w, w));

  return dp[i][k] = ans;
}

ll t[400008];
void build(vector<ll> &a, int v, int tl, int tr) {
  if (tl == tr) {
    t[v] = a[tl];
  } else {
    int tm = (tl + tr) / 2;
    build(a, v * 2, tl, tm);
    build(a, v * 2 + 1, tm + 1, tr);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}

ll maxi(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    return t[v];
  }
  int tm = (tl + tr) / 2;
  return max(maxi(v * 2, tl, tm, l, min(r, tm)),
             maxi(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}

void update(int v, int tl, int tr, int pos, ll new_val) {
  if (tl == tr) {
    t[v] = new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2, tl, tm, pos, new_val);
    else
      update(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}

ll divi[10000009] = {1};
void sieve() {
  for (int i = 2; i < 10000009; i++) {
    divi[i]++;
    for (int j = i * 2; j * j <= 10000009; j += i) {
      divi[j]++;
    }
  }
}

int main() {

  sieve();
  int i;

  cin >> i;
  cout << divi[i];

  return 0;
}