#include #define rep(i, n) for (int i = 0; i < (int)(n); ++i) using namespace std; using ll = long long; int main() { int N; cin >> N; vector A(N); rep (i, N) cin >> A[i]; sort(A.begin(), A.end()); ll ans = 0; rep (i, N) ans += A[i] % A[(i + 1) % N]; cout << ans << endl; }