forked from prasadgujar/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityqueue.cpp
More file actions
68 lines (63 loc) · 762 Bytes
/
Copy pathpriorityqueue.cpp
File metadata and controls
68 lines (63 loc) · 762 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <bits/stdc++.h>
using namespace std;
void solve(int a[],int n)
{
priority_queue<int> pq;
for(int i=0;i<n;i++)
{
pq.push(a[i]);
}
if(n==1)
{
cout<<a[0]<<"\n";
}
else if(n==2)
{
if(a[0]==a[1])
{
cout<<0<<"\n";
}
else
{
cout<<abs(a[0]-a[1])<<"\n";
}
}
else
{
bool ok = false;
while(ok==false)
{
int a=pq.top();
pq.pop();
int b = pq.top();
pq.pop();
//cout<<a<<" "<<b;
if(a!=b)
{
pq.push(abs(a-b));
}
if(pq.size()==0)
{
cout<<0<<"\n";
ok = true;
}
else if(pq.size()==1)
{
cout<<pq.top()<<"\n";
pq.empty();
ok = true;
}
}
}
}
int main() {
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
solve(a,n);
return 0;
}