adding multiplication of number of times 1 , 2 , . . , K - 1 , 0 sequence occurs and sum of first k natural number and sequence from case 1.
$ ans = ( $ K * ( $ K - 1 ) / 2 ) * $ y + ( $ x * ( $ x + 1 ) ) / 2 ; return $ ans ; }