1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import sys n = int(sys.stdin.readline().rstrip()) dp = [[1e9] * n for _ in range(n)] arr = list(sys.stdin.readline().rstrip()) for i in range(n): for j in range(i, n): if i == 0: if arr[j] == ‘O’: dp[i][j] = j * j continue if (arr[i] == ‘B’ and arr[j] == ‘O’) or (arr[i] == ‘O’ and arr[j] == ‘J’) or ( arr[i] == ‘J’ and arr[j] == ‘B’): dp[i][j] = min(dp[i – 1][j], dp[i][i] + pow(i – j, 2)) continue dp[i][j] = dp[i – 1][j] if n == 1: print(0) exit() print(dp[n – 1][n – 1] if dp[n – 1][n – 1] != 1e9 else –1) | cs |