Problem p02881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

typedef long long LL;
typedef vector<LL> VL;
typedef vector<vector<LL>> VVL;
typedef vector<string> VS;

#define REPD(i, a, b) for (LL i = (a); i < (b); i++)
#define REP(i, n) REPD(i, 0, n)

LL llRead() {
  LL a;
  cin >> a;
  return a;
};

string stringRead() {
  string a;
  cin >> a;
  return a;
};

vector<LL> vlRead(LL a) {
  vector<LL> rv;
  REP(i, a) {
    LL input;
    cin >> input;
    rv.push_back(input);
  }
  return rv;
}

vector<string> vsRead(LL a) {
  vector<string> rv;
  REP(i, a) {
    string input;
    cin >> input;
    rv.push_back(input);
  }
  return rv;
}
vector<vector<LL>> vvlRead(LL a, LL b) {
  vector<vector<LL>> rv;
  REP(i, a) {
    rv.push_back(vector<LL>());
    REP(j, b) {
      LL input;
      cin >> input;
      rv.at(i).push_back(input);
    }
  }
  return rv;
}

#if 0
#define debug(d) debug1(__LINE__, #d, d)
void debug1(LL line, const char* name, VL& v) {
  cout << line << ":" << name << ":vector<LL>(" << v.size() << ") ";
  REP(i, (LL)v.size()) {
    cout << v.at(i) << " ";
  }
  cout << endl;
}
void debug1(LL line, const char* name, LL i) {
  cout << line << ":" << name << ":LL " << i << endl;;
}
void debug1(LL line, const char* name, VS& v) {
  cout << line << ":" << name << ":vector<string>(" << v.size() << ") ";
  REP(i, (LL)v.size()) {
    cout << v.at(i) << " ";
  }
  cout << endl;
}
void debug1(LL line, const char* name, VVL& v) {
  cout << line << ":" << name << ":vector<vector<LL>>(" << v.size() << ") ";
  REP(i, (LL)v.size()) {
    REP(j, (LL)v.at(i).size()) {
      cout << v.at(i).at(j) << " ";
    }
    cout << ", ";
  }
  cout << endl;
}
void debug1(LL line, const char* name, char* s) {
  cout << line << ":" << name << ":char* " << s << endl;;
}
void debug1(LL line, const char* name, vector<const char*>& v) {
  cout << line << ":" << name << ":vector<const char*>(" << v.size() << ") ";
  REP(i, (LL)v.size()) {
    cout << v.at(i) << " ";
  }
  cout << endl;
}
void debug1(LL line, const char* name, priority_queue<LL>& p) {
  cout << line << ":" << name << ":priority_queue<LL>(" << p.size() << ") ";
  priority_queue<LL> copy = p;
  while(!copy.empty()) {
    cout << copy.top() << " ";
    copy.pop();
  }
  cout << endl;
}
#else
#define debug(n)
#endif

int main(int argc, const char *argv[]) {
  cout.precision(16);
  // source code

  auto n = llRead();
  VL s;
  LL c = 2;
  LL n2 = n;
  LL sn = sqrt(n);
  while (true) {
    if (n2 == 1)
      break;
    if (c > sn) {
      s.push_back(n2);
      break;
    }
    if ((n2 / c) * c == n2) {
      s.push_back(c);
      n2 = n2 / c;
      continue;
    }
    c++;
  }
  debug(s);
#if 0
  LL a = 1;
  LL b = 1;
  while(!s.empty()) {
    LL cu = s.back();
    s.pop_back();
    if (b > a) a = a*cu;
    else b = b*cu;
  }
  cout << (a-1) + (b-1) << endl;
#endif

  LL a = 1;
  LL b = 1;
  LL min = 10000000000000L;
  function<void(LL)> check = [&](LL i) {
    if (a + b > min)
      return;
    if (i < s.size()) {
      a *= s.at(i);
      check(i + 1);
      a /= s.at(i);
      b *= s.at(i);
      check(i + 1);
      b /= s.at(i);
    } else {
      if (a + b < min)
        min = a + b;
    }
  };
  check(0);
  cout << min - 2 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

typedef long long LL;
typedef vector<LL> VL;
typedef vector<vector<LL>> VVL;
typedef vector<string> VS;

#define REPD(i, a, b) for (LL i = (a); i < (b); i++)
#define REP(i, n) REPD(i, 0, n)

LL llRead() {
  LL a;
  cin >> a;
  return a;
};

string stringRead() {
  string a;
  cin >> a;
  return a;
};

vector<LL> vlRead(LL a) {
  vector<LL> rv;
  REP(i, a) {
    LL input;
    cin >> input;
    rv.push_back(input);
  }
  return rv;
}

vector<string> vsRead(LL a) {
  vector<string> rv;
  REP(i, a) {
    string input;
    cin >> input;
    rv.push_back(input);
  }
  return rv;
}
vector<vector<LL>> vvlRead(LL a, LL b) {
  vector<vector<LL>> rv;
  REP(i, a) {
    rv.push_back(vector<LL>());
    REP(j, b) {
      LL input;
      cin >> input;
      rv.at(i).push_back(input);
    }
  }
  return rv;
}

#if 0
#define debug(d) debug1(__LINE__, #d, d)
void debug1(LL line, const char* name, VL& v) {
  cout << line << ":" << name << ":vector<LL>(" << v.size() << ") ";
  REP(i, (LL)v.size()) {
    cout << v.at(i) << " ";
  }
  cout << endl;
}
void debug1(LL line, const char* name, LL i) {
  cout << line << ":" << name << ":LL " << i << endl;;
}
void debug1(LL line, const char* name, VS& v) {
  cout << line << ":" << name << ":vector<string>(" << v.size() << ") ";
  REP(i, (LL)v.size()) {
    cout << v.at(i) << " ";
  }
  cout << endl;
}
void debug1(LL line, const char* name, VVL& v) {
  cout << line << ":" << name << ":vector<vector<LL>>(" << v.size() << ") ";
  REP(i, (LL)v.size()) {
    REP(j, (LL)v.at(i).size()) {
      cout << v.at(i).at(j) << " ";
    }
    cout << ", ";
  }
  cout << endl;
}
void debug1(LL line, const char* name, char* s) {
  cout << line << ":" << name << ":char* " << s << endl;;
}
void debug1(LL line, const char* name, vector<const char*>& v) {
  cout << line << ":" << name << ":vector<const char*>(" << v.size() << ") ";
  REP(i, (LL)v.size()) {
    cout << v.at(i) << " ";
  }
  cout << endl;
}
void debug1(LL line, const char* name, priority_queue<LL>& p) {
  cout << line << ":" << name << ":priority_queue<LL>(" << p.size() << ") ";
  priority_queue<LL> copy = p;
  while(!copy.empty()) {
    cout << copy.top() << " ";
    copy.pop();
  }
  cout << endl;
}
#else
#define debug(n)
#endif

int main(int argc, const char *argv[]) {
  cout.precision(16);
  // source code

  auto n = llRead();
  VL s;
  LL c = 2;
  LL n2 = n;
  LL sn = sqrt(n);
  while (true) {
    if (n2 == 1)
      break;
    if (c > sn) {
      s.push_back(n2);
      break;
    }
    if ((n2 / c) * c == n2) {
      s.push_back(c);
      n2 = n2 / c;
      continue;
    }
    c++;
  }
  debug(s);
#if 0
  LL a = 1;
  LL b = 1;
  while(!s.empty()) {
    LL cu = s.back();
    s.pop_back();
    if (b > a) a = a*cu;
    else b = b*cu;
  }
  cout << (a-1) + (b-1) << endl;
#endif

  LL a = 1;
  LL b = 1;
  LL min = 10000000000000L;
  function<void(LL)> check = [&](LL i) {
    if (a + b > min)
      return;
    if (i < s.size()) {
      LL cn = 1;
      for (LL j = i + 1; j < s.size(); j++) {
        if (s.at(i) == s.at(j)) {
          cn++;
        } else {
          break;
        }
      }
      if (cn == 1) {
        a *= s.at(i);
        check(i + 1);
        a /= s.at(i);
        b *= s.at(i);
        check(i + 1);
        b /= s.at(i);
      } else {
        debug(i);
        debug(cn);
        for (LL k = 0; k < cn; k++) {
          LL ba = a;
          LL bb = b;
          for (LL l = 0; l < k; l++) {
            a *= s.at(i);
          }
          for (LL l = 0; l < cn - k; l++) {
            b *= s.at(i);
          }
          check(i + cn);
          a = ba;
          b = bb;
        }
      }
    } else {
      if (a + b < min)
        min = a + b;
    }
  };
  check(0);
  cout << min - 2 << endl;
  return 0;
}