DP or powers of 11


#1

DP is good and it gives N^2 complexity.
If its known that A is not too large, we could also try with powers of 11! that could be done in logarithmatic time.