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
#include <iostream>
    #include <stdio.h>
    using namespace std;
   

  const int N = 5000;
  const int MAX = N * N + 10;

  int numbers[N];

  int bigger(int a, int b) {
    return a > b ? a : b;
  }

  void swapNumbers(int i, int j) {
    int number = numbers[i];
    numbers[i] = numbers[j];
    numbers[j] = number;
  }

  void sortNumbers(int leftBound, int rightBound) {
    if (leftBound >= rightBound) {
      return;
    }
    int left = leftBound;
    int right = rightBound;

    while (left < right) {
      if (numbers[left] < numbers[left + 1]) {
        swapNumbers(left + 1, right);
        right--;
      } else {
        swapNumbers(left, left + 1);
        left++;
      }
    }
    sortNumbers(leftBound, left - 1);
    sortNumbers(right + 1, rightBound);

  }

  int main() {
    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
      cin >> numbers[i];
    }
    sortNumbers(0, n - 1);

    int histogram[MAX];
    for (int i = 0; i < MAX; i++) {
      histogram[i] = 0;
    }

    int max = 0;
    histogram[0] = 1;
    for (int iNumber = 0; iNumber < n; iNumber++) {
      int number = numbers[iNumber];
      for (int i = max; i + 1 >= number; i--) {
        histogram[i + number] += histogram[i];
        histogram[i + number] %= 1000000007;
        max = bigger(i + number, max);
      }
    }

    int sum = -1;
    for (int i = 0; i < MAX; i++) {
      sum += histogram[i];
      sum %= 1000000007;
    }

    printf("%d", sum);
    return 0;
  }