-
Notifications
You must be signed in to change notification settings - Fork 59
/
banuti.cpp
73 lines (66 loc) · 1.52 KB
/
banuti.cpp
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
69
70
71
72
73
# include <bits/stdc++.h>
# define INF 1e9 + 5
# define NR 50005
using namespace std;
ifstream f("banuti.in");
ofstream g("banuti.out");
struct elem2 {
int nr, rest;
}st[NR];
queue <int> q;
int i,j,n,m,minn,VV,sol;
int a[NR], rest[NR], M[NR], luat[NR], minim[NR];
void resturi () {
for (int i=1; i<=2*minn; ++i)
rest[i]=i%minn;
}
void Djikstra () {
int X, k, Xurm, kurm;
minim[0]=M[0]; luat[0]=1;
for (int i=1; i<minn; ++i) {
luat[i]=0; minim[i]=M[i];
}
q.push(0);
while (! q.empty()) {
k=q.front(); q.pop();
for (int i=1; i<=VV; ++i) {
Xurm=minim[k] + st[i].nr;
kurm=rest[k + st[i].rest];
if (! luat[kurm] && Xurm < minim[kurm]) {
minim[kurm]=Xurm;
}
}
int MINN=INF, I;
for (int i=0; i<minn; ++i) {
if (! luat[i] && minim[i]<MINN)
MINN=minim[i], I=i;
}
if (MINN!=INF) {
luat[I]=1;
q.push(I);
}
}
}
int main ()
{
f>>n;
for (i=1; i<=n; ++i)
f>>a[i];
sort (a+1, a+n+1);
minn=a[1]; resturi ();
for (i=0; i<minn; ++i) M[i]=INF;
for (i=1; i<=n; ++i)
M[a[i]%minn]=min(M[a[i]%minn], a[i]);
for (i=0; i<minn; ++i)
if (M[i]!=INF) {
++VV;
st[VV].nr=M[i]; st[VV].rest=i;
}
Djikstra ();
sol=0;
for (i=0; i<minn; ++i)
sol=max(sol, minim[i]);
if (sol==INF) g<<"-1\n";
else g<<sol-minn<<"\n";
return 0;
}