CCCGuide

Problem S5: Factor Solitaire

2013 · S5 · Original problem · DMOJ

Problem Description

In the game of Factor Solitaire, you start with the number 1, and try to change it to some given target number nn by repeatedly using the following operation. In each step, if cc is your current number, you split it into two positive factors aa, bb of your choice such that c=a∗bc=a*b. You then add aa to your current number cc to get your new current number. Doing this costs you bb points. You continue doing this until your current number is nn, and you try to achieve this at the cost of a minimum total number of points.

For example, here is one way to get to 15:

In fact, this is the minimum possible total cost to get 15. You want to compute the minimum total cost for other target end numbers.

Input Specification

The input consists of a single integer N≥1N \ge 1. In at least half of the cases N≤50000N \le 50000, in at least another quarter of the cases N≤500000N \le 500000, and in the remaining cases N≤5000000N \le 5000000.

Output Specification

Compute the minimum cost that gets you to NN.

Sample Input 1

15

Output for Sample Input 1

9

Sample Input 2

2013

Output for Sample Input 2

91

Explanation of Output for Sample Input 2

For example, start with 1, then get to 2, 4, 5, 10, 15, 30, 60, 61, 122, 244, 305, 610, 671, 1342, and then 2013.