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
#include<cstdio>
#include<vector>
#include<map>
#include<algorithm>

int main() {

    long long P = 1e9 + 7;
    int N;
    scanf("%d", &N);

    std::vector<int> a(N, 0);
    for (int i = 0; i < N; ++i) {
        scanf("%d", &a[i]);
    }

    std::sort(a.begin(), a.end());

    if (a[0] > 1) {
        puts("0");
        return 0;
    }

    long long ret = 0;
    std::map<int, long long> d;
    d[0] = 1;

    for (int i = 0; i < N; ++i) {
        auto it = d.rbegin();

        if (it->first < a[i] - 1) break;

        while (it != d.rend() && it->first >= a[i] - 1) {
            auto prev = it++;
            ret = (ret + prev->second) % P;

            int v = std::min(prev->first + a[i], 5000);
            auto next = d.find(v);
            if (next != d.end()) {
                next->second = (next->second + prev->second) % P;
            } else {
                d.emplace(v, prev->second);
            }
        }

        if (it != d.rend())
            d.erase(d.begin(), it.base());

        //printf("----- %d\n", d.size());
        //for (auto m : d)
        //    printf("%d -> %d\n", m.first, m.second);

    }

    printf("%lld\n", ret);

    return 0;
}