#include using namespace std; typedef long long ll; #define all(x) (x).begin(),(x).end() const int mod=1000000007,MAX=1<<18; const ll INF=1LL<<55; int main(){ int N;cin>>N; priority_queue PQ; ll sum=0; for(int i=0;i>Q; for(int i=0;i=a){ ll b=PQ.top();PQ.pop(); sum-=b/a*a; PQ.push(b%a); } printf("%lld\n",sum); } }