Adding subrectangles

Given a grid with uppercase letters, compute the sum of the values of the letters in each rectangle with a corner in the upper-left extreme. The values of the letters are ’A’=1=1, ’B’=1+2=3=1+2=3, ’C’=1+2+3=6=1+2+3=6, ’D’=1+2+3+4=10=1+2+3+4=10, etcetera, up to ’Z’=351=351.

Input

Input consists of 0<r5000< r\leq 500 lines, all of them with the same number of uppercase letters 0<c5000<c\leq 500.

Output

Print rr lines with cc numbers each. The jj-th number of the ii-th row must be the sum of the values of the letters of the rectangle whose corners are the first letter of the input, and the jj-th letter of the ii-th row.

Hint

Consider using the inclusion-exclusion principle.

Problem information

Author: Unknown
Translator: Salvador Roura

Generation: 2026-01-25T12:16:15.416Z

© Jutge.org, 2006–2026.
https://jutge.org