n,k=map(int,input().split()) a=list(map(int,input().split())) dec=0 for i in range(n): dec+=k**i * a[-i-1] print(dec%sum(a))