Problem p03913 (C++)

Buggy submission (status: Time Limit Exceeded):
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
// #pragma GCC target("avx,tune=native")
#include <bits/stdc++.h>
#define int long long
// #include<ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace __gnu_pbds;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 1000000007;
typedef long long ll;
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
inline string getstr(string &s, int l, int r) {
  string ret = "";
  for (int i = l; i <= r; i++)
    ret.push_back(s[i]);
  return ret;
}
int modpow(int x, int y, int md = mod) {
  if (y == 0)
    return 1;
  int ret = modpow(x, y >> 1, md);
  ret = (ll)ret * ret % md;
  if (y & 1)
    ret = (ll)ret * x % md;
  return ret;
}
int n, a, ans = 1e18;
const int big = 1e18;
signed main() {
  cin >> n >> a;
  ans = n;
  for (int i = 1;; i++) {
    //		cerr<<i<<" "<<ans<<endl;
    if (ans < a * i)
      break;
    int len = i + 1;
    int v;
    for (int j = 2;; j++) {
      //			cerr<<j<<endl;
      int now = 1;
      bool ok = false;
      for (int k = 1; k <= len; k++) {
        if (now > big / j)
          ok = true;
        now = now * j;
      }
      ok |= (now >= n);
      if (ok) {
        v = j;
        break;
      }
    }
    for (int j = 0; j <= len; j++) {
      int mul1 = 1, mul2 = 1;
      for (int k = 1; k <= j; k++) {
        mul1 *= v;
      }
      for (int k = j + 1; k <= len; k++) {
        mul2 *= (v - 1);
      }
      if (mul1 > big / mul2 || mul1 * mul2 >= n) {
        ans = min(ans, a * i + v * j + (v - 1) * (len - j));
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
// #pragma GCC target("avx,tune=native")
#include <bits/stdc++.h>
#define int long long
// #include<ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace __gnu_pbds;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 1000000007;
typedef long long ll;
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
inline string getstr(string &s, int l, int r) {
  string ret = "";
  for (int i = l; i <= r; i++)
    ret.push_back(s[i]);
  return ret;
}
int modpow(int x, int y, int md = mod) {
  if (y == 0)
    return 1;
  int ret = modpow(x, y >> 1, md);
  ret = (ll)ret * ret % md;
  if (y & 1)
    ret = (ll)ret * x % md;
  return ret;
}
int n, a, ans = 1e18;
const int big = 1e18;
signed main() {
  cin >> n >> a;
  ans = n;
  for (int i = 1; i <= 1000; i++) {
    //		cerr<<i<<" "<<ans<<endl;
    if (ans < a * i)
      break;
    int len = i + 1;
    int v;
    for (int j = 2;; j++) {
      //			cerr<<j<<endl;
      int now = 1;
      bool ok = false;
      for (int k = 1; k <= len; k++) {
        if (now > big / j)
          ok = true;
        now = now * j;
      }
      ok |= (now >= n);
      if (ok) {
        v = j;
        break;
      }
    }
    for (int j = 0; j <= len; j++) {
      int mul1 = 1, mul2 = 1;
      for (int k = 1; k <= j; k++) {
        mul1 *= v;
      }
      for (int k = j + 1; k <= len; k++) {
        mul2 *= (v - 1);
      }
      if (mul1 > big / mul2 || mul1 * mul2 >= n) {
        ans = min(ans, a * i + v * j + (v - 1) * (len - j));
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}