using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("4 2 2");
WillReturn.Add("1 2 3 4");
//6
}
else if (InputPattern == "Input2") {
WillReturn.Add("3 1 2");
WillReturn.Add("1 3 5");
//-1
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
long K = wkArr[1];
long D = wkArr[2];
long[] AArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
// 和[個数 , Dで割った余り]なDP表
long?[,] DPArr = new long?[K + 1, D + 1];
DPArr[0, 0] = 0;
foreach (long EachA in AArr) {
for (long I = K; 0 <= I; I--) {
for (long J = D; 0 <= J; J--) {
if (DPArr[I, J].HasValue == false) continue;
long NewI = I + 1;
if (NewI > K) continue;
long NewJ = (DPArr[I, J].Value + EachA) % D;
long NewVal = DPArr[I, J].Value + EachA;
if (DPArr[NewI, NewJ].HasValue) {
if (DPArr[NewI, NewJ].Value >= NewVal) {
continue;
}
}
DPArr[NewI, NewJ] = NewVal;
}
}
}
if (DPArr[K, 0].HasValue) {
Console.WriteLine(DPArr[K, 0]);
}
else {
Console.WriteLine(-1);
}
}
}