#include #include #include #include using namespace std; long lSqrt (long N) { if (N <= 1) return N; long maxSqrt = 1; while (maxSqrt * maxSqrt <= N) { maxSqrt *= 2L; } long minSqrt = maxSqrt / 2L; while (minSqrt < maxSqrt) { long mid = (minSqrt + maxSqrt) / 2L; long sqr = mid*mid; if (sqr == N) return mid; else if (sqr < N) { minSqrt = mid + 1; } else maxSqrt = mid; } return minSqrt-1L; } long pack(long N) { long bestSoFar = numeric_limits::max(); long stop = lSqrt(N)+1; long start = max(1L, 7 * (stop-1L) / 10); // slightly less than sqrt(2.0) * stop for (long h = start; h <= stop; ++h) { long w = N / h; if (2*h >= w) { if (h*w == N) { bestSoFar = 0; //cerr << N << " can be factored as " << h << " by " << w << endl; break; } else { ++w; if (2*h >= w) { long boxSize = h*w; bestSoFar = min(bestSoFar, boxSize - N); /* cerr << "Looked at " << h << " by " << w << " = " << boxSize << endl; cerr << "best so far: " << bestSoFar << endl; */ } } } } return bestSoFar; } void solve (istream& in) { //cerr << "Accepting input up to " << numeric_limits::max() << endl; long N; in >> N; long empty = pack(N); cout << empty << endl; } int main (int argc, char** argv) { if (argc > 1) { ifstream in (argv[1]); solve (in); } else solve (cin); }