#include using namespace std; int main() { int N, K; cin >> N >> K; vector A( N ); for( int i = 0; i < N; i++ ) cin >> A[i]; long long s = 0; long long a = 0; long long k = 1; for( int i = N - 1; i >= 0; i-- ) { s += A[i]; a += A[i] * k; k *= K; } long long ans = a % s; cout << ans << endl; }