Maximum possible sum of squares of stack elements satisfying the given properties | Function to find the maximum sum of squares of stack elements ; Stores the sum ofsquares of stack elements ; Check if sum is valid ; Initialize all stack elements with 1 ; Stores the count the number of stack elements not equal to 1 ; Add the sum of squares of stack elements not equal to 1 ; Add 1 * 1 to res as the remaining stack elements are 1 ; Print the result ; Driver Code ; Function call
def maxSumOfSquares ( N , S ) : NEW_LINE INDENT res = 0 NEW_LINE if ( S < N or S > 9 * N ) : NEW_LINE INDENT cout << - 1 ; NEW_LINE return NEW_LINE DEDENT S = S - N NEW_LINE c = 0 NEW_LINE while ( S > 0 ) : NEW_LINE INDENT c += 1 NEW_LINE if ( S // 8 > 0 ) : NEW_LINE INDENT res += 9 * 9 NEW_LINE S -= 8 NEW_LINE DEDENT else : NEW_LINE INDENT res += ( S + 1 ) * ( S + 1 ) NEW_LINE break NEW_LINE DEDENT DEDENT res = res + ( N - c ) NEW_LINE print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE S = 12 NEW_LINE maxSumOfSquares ( N , S ) NEW_LINE DEDENT