#include #define NIL (-1) #define ll long long using namespace std; const double PI = acos(-1.0); int main() { ll N, K; cin >> N >> K; ll n = 0LL, c = 1; ll s = 0; vector A(N); for (int i = 0; i < N; i++) { cin >> A[i]; s += A[i]; } reverse(A.begin(),A.end()); for (int i = 0; i < N; i++) { n += A[i] * c; n %= s; c *= K; c %= s; } cout << n%s << endl; return 0; }