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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdint.h>
#include <iostream>

#define BUFFER 786432
int64_t nums[BUFFER];
int primes[100];
int prime_count;

int pointers[100];
int64_t multed[100];

int64_t find_ascending(int64_t maxnum) {
    nums[0] = 1;
    int num_ndx = 0;
    int64_t prev = 1;

    for (int i = 0; i < prime_count; i++) {
        multed[i] = primes[i];
    }

    while (true) {
        int64_t next = 9223372036854775806;
        for (int i = 0; i < prime_count; i++) {
            if (multed[i] < next) {
                next = multed[i];
            }
        }
        if (next > maxnum) {
            return prev;
        }
        prev = next;
        if (num_ndx < BUFFER) {
            num_ndx += 1;
            nums[num_ndx] = next;
        }
        for (int i = 0; i < prime_count; i++) {
            if (multed[i] == next) {
                pointers[i] += 1;
                if (pointers[i] >= BUFFER) {
                    return -1;
                }
                multed[i] = primes[i] * nums[pointers[i]];
            }
        }
    }
}

bool allowed(uint64_t num) {
    for (int i = 0; i < prime_count; i++) {
        while ((num % primes[i]) == 0) {
            num /= primes[i];
        }
    }
    return num == 1;
}

int64_t find_descending(uint64_t maxnum) {
    while (maxnum > 0) {
        if (allowed(maxnum)) {
            return maxnum;
        }
        maxnum -= 1;
    }
}

int main() {
    uint64_t maxnum;

    std::cin >> prime_count;
    std::cin >> maxnum;

    for (int i = 0; i < prime_count; i++) {
        std::cin >> primes[i];
    }

    int64_t result = -1;

    if (result < 0) {
        result = find_ascending(maxnum);
    }

    if (result < 0) {
        result = find_descending(maxnum);
    }

    std::cout << result;
}