cho.sh
Notes
Loading...

Hexagonal Numbers

Time limit

2s

Memory limit

128 MB

Problem

Hexagonal numbers can be defined by arranging points in hexagonal shapes. Let hnh_nhn​ be the number of distinct points that appear when hexagons with 1, 2, ..., nnn points on a side are drawn so that only one point overlaps.

The figure shows h1h_1h1​, h2h_2h2​, h3h_3h3​, and h4h_4h4​ in order. The first six hexagonal numbers are 1, 6, 15, 28, 45, and 66.

Given a positive integer NNN, find the minimum number of hexagonal numbers whose sum is NNN.

NMinimum countSum
111
221+1
331+1+1
441+1+1+1
551+1+1+1+1
616
721+6
831+1+6
941+1+1+6
1051+1+1+1+6
1161+1+1+1+1+6
1226+6

Every integer greater than 1791 can be represented as the sum of four hexagonal numbers. Also, every sufficiently large number can be represented as the sum of three hexagonal numbers. For every positive integer, the minimum count is at most 6, and only 11 and 26 have minimum count 6. The largest number with answer 6 is 26, the largest number with answer 5 is 130, and the largest number with answer 4 is 146858.

Input

The first line contains the positive integer NNN.

Output

Print the minimum number of hexagonal numbers needed to make NNN.

Constraints

  • 1≤N≤1,000,0001 \le N \le 1,000,0001≤N≤1,000,000