#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using PII = pair<int, int>;
int main()
{
const int MOD = 1000000007;
int n;
cin >> n;
vector<int> packs(n);
for (int i = 0; i < n; ++i)
{
cin >> packs[i];
}
sort(packs.begin(), packs.end());
vector<int> cnt(5001, 0);
cnt[0] = 1;
int res = 0;
int cnt_max = 0;
for (int i = 0; i < n; ++i)
{
if (cnt_max > 0)
{
res = (res + cnt_max) % MOD;
cnt_max = (cnt_max + cnt_max) % MOD;
}
for (int j = 5000; j >= packs[i] - 1; --j)
{
if (cnt[j] > 0)
{
res = (res + cnt[j]) % MOD;
int ind = j + packs[i];
if (ind > 5000)
cnt_max = (cnt_max + cnt[j]) % MOD;
else
cnt[ind] = (cnt[ind] + cnt[j]) % MOD;
}
}
}
cout << res << endl;
return 0;
}
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 <algorithm> #include <iostream> #include <vector> using namespace std; using PII = pair<int, int>; int main() { const int MOD = 1000000007; int n; cin >> n; vector<int> packs(n); for (int i = 0; i < n; ++i) { cin >> packs[i]; } sort(packs.begin(), packs.end()); vector<int> cnt(5001, 0); cnt[0] = 1; int res = 0; int cnt_max = 0; for (int i = 0; i < n; ++i) { if (cnt_max > 0) { res = (res + cnt_max) % MOD; cnt_max = (cnt_max + cnt_max) % MOD; } for (int j = 5000; j >= packs[i] - 1; --j) { if (cnt[j] > 0) { res = (res + cnt[j]) % MOD; int ind = j + packs[i]; if (ind > 5000) cnt_max = (cnt_max + cnt[j]) % MOD; else cnt[ind] = (cnt[ind] + cnt[j]) % MOD; } } } cout << res << endl; return 0; } |
English