fork(1) download
  1. #include<iostream>
  2. #include<set>
  3. using namespace std;
  4.  
  5. int maxi(int a,int b)
  6. {
  7. if(a>b)
  8. return a;
  9. else
  10. return b;
  11. }
  12.  
  13. int mini(int a,int b)
  14. {
  15. if(a>b)
  16. return b;
  17. else
  18. return a;
  19. }
  20.  
  21. int main()
  22. {
  23. int a,b,c,x,y;
  24. here:;
  25. while(cin>>a>>b>>c>>x>>y)
  26. {
  27.  
  28. if(a==0 && b==0 && c==0 && x==0 && y==0)
  29. return 0;
  30. int max=maxi(x,y),min=mini(x,y);
  31. set<int> pr,res;
  32. pr.insert(a); pr.insert(b); pr.insert(c); pr.insert(x); pr.insert(y);
  33.  
  34. if(x>=maxi(maxi(a,b),c) && y>=maxi(maxi(a,b),c))
  35. for(int i=1;i<53;i++)
  36. if(pr.find(i)==pr.end())
  37. {cout<<i<<endl; goto here;}
  38.  
  39. bool lost=false;
  40. for(int i=1;i<=6;i++)
  41. {
  42. int m,n,l;
  43.  
  44. if(i==1)
  45. {m=a; n=b; l=c;}
  46.  
  47. if(i==2)
  48. {m=a; n=c; l=b;}
  49.  
  50. if(i==3)
  51. {m=b; n=a; l=c;}
  52.  
  53. if(i==4)
  54. {m=b; n=c; l=a;}
  55.  
  56. if(i==5)
  57. {m=c; n=a; l=b;}
  58.  
  59. if(i==6)
  60. {m=c; n=b; l=a;}
  61. //////////////////////////
  62. if(m>max && n>min)
  63. {cout<<-1<<endl; lost=true; break;}
  64.  
  65. bool na=true;
  66. if(m<=min && n<=min)
  67. for(int j=maxi(m,n);j<max;j++)
  68. if(pr.find(j)==pr.end())
  69. {res.insert(j); na=false; break;}
  70. if(!na)
  71. break;
  72.  
  73. for(int j=l;j<53;j++)
  74. if(pr.find(j)==pr.end())
  75. {res.insert(j); na=false; break;}
  76.  
  77. if(na)
  78. {cout<<-1<<endl; lost=true; break;}
  79. }
  80.  
  81. if(!res.empty() && !lost)
  82. cout<<*res.rbegin()<<endl;
  83. }
  84. }
Success #stdin #stdout 0s 3036KB
stdin
11 17 4 16 39
32 18 14 8 30
50 46 51 23 5
44 32 45 40 21
34 43 52 37 27
19 27 23 18 52
1 4 17 32 47
31 12 41 44 47
18 42 40 16 40
44 35 19 12 22
16 45 40 15 5
42 34 32 40 27
31 41 6 23 20
1 1 7 41 45
1 6 10 40 49
49 7 31 44 18
28 35 38 15 49
43 4 6 50 43
8 4 7 14 3
27 42 3 9 6
23 10 39 8 25
11 33 32 17 52
25 44 34 11 6
6 29 37 12 26
3 47 5 10 8
7 12 25 38 48
6 8 33 44 44
6 2 24 13 18
23 37 37 4 23
18 9 51 2 48
52 33 43 5 18
26 39 5 27 24
52 32 8 9 52
51 42 1 50 2
47 20 14 31 51
13 49 35 11 26
7 11 34 49 43
51 50 29 31 52
29 7 8 12 15
7 10 4 7 35
33 29 2 22 36
28 34 8 10 21
33 16 31 43 40
49 17 14 1 48
13 29 2 20 40
44 26 25 47 9
7 3 37 36 24
20 11 34 27 21
2 36 36 8 2
52 4 18 13 32
41 25 9 42 21
24 33 22 25 3
30 31 5 43 43
5 38 1 38 41
49 15 52 9 22
1 8 1 18 20
32 7 20 16 24
40 40 33 10 12
11 15 18 16 5
8 48 19 37 33
7 33 23 6 41
44 6 48 44 51
15 51 33 35 15
33 50 30 13 35
41 23 50 34 14
30 42 9 48 26
17 30 34 39 35
23 6 16 18 25
43 9 24 23 19
38 3 16 15 43
51 31 42 24 12
3 29 29 40 1
30 32 30 12 47
13 34 52 4 51
25 46 35 24 45
1 9 23 17 51
14 43 29 3 14
16 33 18 45 48
18 50 28 48 9
22 36 42 49 39
17 21 9 51 44
1 52 28 23 44
2 12 34 6 14
23 22 23 16 42
18 34 39 45 29
24 42 40 13 39
2 5 35 10 32
3 38 7 6 9
50 8 20 7 13
10 5 10 32 20
27 49 29 42 18
5 13 7 20 1
21 22 6 4 7
37 34 45 19 39
1 16 46 48 50
35 5 30 44 12
25 19 37 2 8
2 34 20 36 2
20 5 23 1 36
5 13 17 49 31
0 0 0 0 0
stdout
12
-1
-1
-1
-1
28
2
1
-1
-1
-1
-1
-1
2
2
-1
39
1
9
-1
-1
34
-1
-1
6
1
1
7
-1
-1
-1
-1
-1
-1
21
-1
1
-1
9
8
34
-1
1
-1
14
45
8
22
-1
-1
43
-1
1
2
-1
2
-1
-1
-1
20
34
45
-1
-1
-1
43
1
17
-1
39
-1
30
33
-1
-1
10
-1
1
-1
37
1
-1
-1
24
40
-1
6
-1
-1
1
-1
14
-1
-1
2
36
-1
35
24
1