# 1047 糖果
# 初始值要考虑空余值是否可以为0,初始状态值是否为0或者1,值是取最大值还是和值
N, K = map(int, input().split())
dp = [[float('-inf') for _ in range(K + 10)] for _ in range(N + 10)]
w = [0 for i in range(N)]
for i in range(N):
w[i] = int(input())
dp[0][0] = 0
for i in range(1, N + 1):
for j in range(K):
dp[i][j] = max(dp[i - 1][j], dp[i - 1][(j - w[i - 1] % K) % K] + w[i - 1])
print(dp[N][0])