Given two natural numbers and , you must construct a number using the digits (exactly one of each) such that no (non-empty) prefix of is a multiple of . For example, with and , is not a valid order, because is a multiple of . In contrast, is a valid order, because neither , nor , nor , nor are multiples of .
Moreover, you have a matrix such that contains the prize that is obtained if digit is placed immediately to the right of digit . Maximize the total sum of the prizes.
The input consists of several cases. Each case starts with and , followed by : rows, each with natural numbers between 1 and , except for the diagonal, that is filled with zeros. You can assume that and .
For each case, print the maximum possible prize. If no can be constructed, print 0.
Author: Unknown
Translator: Albert Oliveras
Generation: 2026-01-25T11:22:09.366Z
© Jutge.org, 2006–2026.
https://jutge.org