Balls
Practice
3.5 (76 votes)
Easy
Problem
79% Success 5689 Attempts 20 Points 2s Time Limit 256MB Memory 1024 KB Max Code

A square pyramid of balls consists of square layers of balls stacked on top of each other. The i th (1-based indexing )layer from the top consists of exactly i2 balls. Image

You have received one such beautiful square pyramid on your birthday, with each layer having a unique color. However, being the clumsy doofus you are, you knocked it over and lost some(maybe all) balls of some(maybe all) layers. You quickly gather the balls and try to reconstruct a square pyramid(maybe of a different height) by using all the recovered balls and replacing the missing balls by new ones from the nearby shop(remember, all layers had unique colors, so you couldn't use balls from one layer in another). Find the minimum number of balls you shall have to purchase.

Input:

You are given an integer N, the number of layers having at-least one recovered ball.

Then N space separated integers follow in the next line, with A[i] representing the number of recovered balls in the ith such layer

Output:

Print the required value.

Constraints:

1 ≤ N ≤ 1,000,000

1 ≤ A[i] ≤ 1,000,000,000

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
14 votes
Tags:
Basic ProgrammingBasics of ImplementationEasyImplementation
Points:30
7 votes
Tags:
BitmaskAlgorithmsEasy-MediumReadyApprovedBit manipulation
Points:50
Tags:
Dynamic ProgrammingLinear AlgebraHardMatrix ExponentiationAlgorithmsMathematicsOpenApproved