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

//
//  main.cpp
//  ABC_172_D
//
//  Created by Joe Mori on 2020/06/27.
//  Copyright © 2020 Joe Mori. All rights reserved.
//

#include <bits/stdc++.h>/*
std::vector<bool>is_prime(10001001, true);
void prime_number(long long int a,long long int N){
    if(is_prime[a]){
        for(long long int i=a*2;i<=N;i+=a){
            is_prime[i]=false;
        }
    }
}*/
int main() {
  // insert code here...
  //    std::vector<long long int>prime(1000001,1);
  //    std::vector<std::vector<long long int> >count(10000001,std::vector<long
  //    long int>(10000001,0));
  /*for(int i=2;i<10000000;i++){
      if(is_prime[i]){
          for(int j=i*2;j<10000000;j+=i){
              is_prime[j]=false;
          }
      }
  }*/
  //    prime[1]=1;
  /*
  long long int sub,yaku=1;
  for(int i=2;i<10000000;i++){
      sub=i;
      for(int j=2;j<=i;j++){
          if(!is_prime[j])break;
          while(sub%j==0){
              sub/=j;
              count[i][j]++;
          }
          yaku=yaku*(count[i][j]+1);
      }
      std::cout<<"yaku:"<<yaku<<std::endl;
      prime[i]=yaku;
      yaku=1;
  }*/
  long long int N, ans = 0, sub, count = 0, sub_ans = 0;
  std::vector<long long int> a(10000010, 0);
  std::cin >> N;
  /*for(int i=2;i<N+1;i++){
      prime_number(i,N);
//        std::cout<<"i=="<<i<<std::endl;
      if(is_prime[i]){
          ans=ans+i*2;
      }else{
          sub=i;
          sub_ans=1;
          for(int j=2;j<=i;j++){
              count=0;
//                std::cout<<"j:"<<j<<std::endl;
              if(!is_prime[j])continue;
//                std::cout<<"j:"<<j<<std::endl;
              while(sub%j==0){
//                    std::cout<<"sub:"<<sub<<std::endl;
                  sub=sub/j;
                  count++;
              }
//                std::cout<<"i:"<<i<<" "<<count<<" j:"<<j<<"
sub:"<<sub<<std::endl; sub_ans=(1+count)*sub_ans; if(sub==1)break;
//                std::cout<<"j:"<<j<<" sub_ans:"<<sub_ans<<std::endl;
          }
//            std::cout<<"i:"<<i<<" sub_ans:"<<i*sub_ans<<std::endl;
          ans=ans+sub_ans*i;
      }
  }
//    DWORD start = timeGetTime();
  //    std::cout<<is_prime[5]<<std::endl;

  std::cout<<ans<<std::endl;*/
  for (int i = 1; i <= N; i++) {
    for (int j = i; j <= N; j += i) {
      a[j]++;
    }
  }
  for (int i = 1; i <= N; i++) {
    ans = ans + a[i] * i;
  }
  std::cout << ans << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

//
//  main.cpp
//  ABC_172_D
//
//  Created by Joe Mori on 2020/06/27.
//  Copyright © 2020 Joe Mori. All rights reserved.
//

#include <bits/stdc++.h>
std::vector<bool> is_prime(10001001, true);
void prime_number(long long int a, long long int N) {
  if (is_prime[a]) {
    for (long long int i = a * 2; i <= N; i += a) {
      is_prime[i] = false;
    }
  }
}
int main() {
  is_prime[0] = false;
  is_prime[1] = false;
  // insert code here...
  //    std::vector<long long int>prime(1000001,1);
  //    std::vector<std::vector<long long int> >count(10000001,std::vector<long
  //    long int>(10000001,0));
  /*for(int i=2;i<10000000;i++){
      if(is_prime[i]){
          for(int j=i*2;j<10000000;j+=i){
              is_prime[j]=false;
          }
      }
  }*/
  //    prime[1]=1;
  /*
  long long int sub,yaku=1;
  for(int i=2;i<10000000;i++){
      sub=i;
      for(int j=2;j<=i;j++){
          if(!is_prime[j])break;
          while(sub%j==0){
              sub/=j;
              count[i][j]++;
          }
          yaku=yaku*(count[i][j]+1);
      }
      std::cout<<"yaku:"<<yaku<<std::endl;
      prime[i]=yaku;
      yaku=1;
  }*/
  long long int N, ans = 1, sub, count = 0, sub_ans = 0;
  std::cin >> N;
  for (int i = 2; i < N + 1; i++) {
    prime_number(i, N);
    //        std::cout<<"i=="<<i<<std::endl;
    if (is_prime[i]) {
      ans = ans + i * 2;
    } else {
      sub = i;
      sub_ans = 1;
      for (int j = 2; j <= i / 2; j++) {
        count = 0;
        //                std::cout<<"j:"<<j<<std::endl;
        if (!is_prime[j])
          continue;
        //                std::cout<<"j:"<<j<<std::endl;

        while (sub % j == 0) {
          //                    std::cout<<"sub:"<<sub<<std::endl;
          sub = sub / j;
          count++;
        }
        //                std::cout<<"i:"<<i<<" "<<count<<" j:"<<j<<"
        //                sub:"<<sub<<std::endl;
        sub_ans = (1 + count) * sub_ans;
        //                std::cout<<"j:"<<j<<" sub_ans:"<<sub_ans<<std::endl;
      }
      //            std::cout<<"i:"<<i<<" sub_ans:"<<i*sub_ans<<std::endl;
      ans = ans + sub_ans * i;
    }
  }
  //    std::cout<<is_prime[5]<<std::endl;
  std::cout << ans << std::endl;
  return 0;
}