Uncle Fyodor's Candies
InterviewTime limit2sMemory limit1024 MB
Given n candies, decide whether they can form a truncated tetrahedron, where consecutive triangular-number layers are stacked with the top layers removed.
- Level
Easy3 of 10
- Topics
- Math, Brute force
- Solved
- No attempts yet
Statement
One sunny Saturday, the villagers of Prostokvashino gathered for tea at Uncle Fyodor's house. This time Uncle Fyodor opened a box of foreign round chocolate candies, and Postman Pechkin, being very tidy, stacked all of them into a regular tetrahedron pyramid.

Each layer of the pyramid is a regular triangle built from candies. The side of each layer holds one candy fewer than the side of the layer above it. The picture below shows the third and fourth layers from the top.

The baby crow Hvatayka did not like how the candies were spread across the layers. The top layer has one candy, and the last one can have ten. He fooled everyone by asking who was there, lowered their guard, and ate several of the top layers whole.

When the others noticed and tried to catch the visibly plump crow, the table was knocked over and all the candies spilled onto the floor.
Sharik and Matroskin searched and collected n candies. They wondered whether any candies were still missing. Nobody remembers how many candies there were, and looking at Hvatayka gave no clue. So the candies are considered not lost if they can be stacked into a truncated tetrahedron pyramid, meaning a pyramid with several top layers removed. Help the people of Prostokvashino find out whether these valuable candies are lost.
Input
The input file contains a single integer n, the number of candies found.
Output
If the n candies can form a truncated pyramid, print YES to the output file. Otherwise, print NO.