# 2019 IUSB Programming Competition # Round 2 Problem 3 # Find the optimal score for the blocks game # Solution by Dana Vrajitoru # Uses Python 3.6.2 # Removes a block from position k def removeBlock(a, k): val = a[k] while k < len(a) and a[k] == val: a.pop(k) # Inserts a block with a given value and length at a given position def insertBlock(a, k, val, size): for i in range(size): a.insert(k, val) # Find the optimal score for an array def optScore(a): n = len(a) start = 0 bestScore = 0 while start < n: # find a block end = start+1 while end < n and a[end] == a[start]: end += 1 # find the score if we remove this block length = end-start value = a[start] score = length*length removeBlock(a, start) score += optScore(a) # then put it back and try the next one insertBlock(a, start, value, length) if score > bestScore: bestScore = score # move to the next block start = end return bestScore # Testing the function if __name__ == '__main__': print("Enter the size and the array:") size = int(input()) str = input().split(" ") a = [] for s in str: a.append(int(s)) print(optScore(a))