@@ -0,0 +1,556 @@
+#!/usr/bin/python%0A%0Aimport sys%0Aimport math%0A%0Adef input_arg(tr=str): return tr(raw_input().strip())%0Adef input_args(tr=str): return map(tr, list(input_arg().split(' ')))%0Adef input_arglines(n,tr=str): return %5Binput_arg(tr) for x in range(n)%5D%0A%0AF = lambda x: int(x)**2%0A%0AK,M = input_args(int)%0ANs = %5B%5D%0Afor k in xrange(K):%0A    N = input_args(lambda x: F(x) %25 M)%0A    N = list(set(N%5B1:%5D))%0A    Ns.append(N)%0A#print Ns%0A%0AS = Ns.pop(0)%0Afor N in Ns:%0A    Snew = %5B%5D%0A    for n in N:%0A        for s in S:%0A            Snew.append((n + s) %25 M)%0A    S = list(set(Snew))%0Aprint max(S)%0A

the trick to maximize-it is: (a + b)%m == (a%m + b%m)%m