Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- long long n, a[15];
- int mask[15];
- int m;
- int if1;
- int ans[15];
- int cnt;
- void gen(int len)
- {
- if (len == m)
- {
- long long sum = 0;
- int ccnt = 0;
- for (int i = 0; i < m; ++i)
- {
- sum += a[i] * mask[i];
- ccnt += mask[i];
- }
- if (sum == n)
- {
- if (ccnt < cnt)
- {
- cnt = ccnt;
- for (int i = 0; i < m; ++i)
- ans[i] = mask[i];
- }
- }
- if (sum > n)
- if1 = 1;
- }
- else
- {
- for (int i = 0; i <= 2; ++i)
- {
- mask[len] = i;
- gen(len + 1);
- }
- }
- }
- int main()
- {
- cnt = 31;
- cin >> n >> m;
- for (int i = 0; i < m; ++i)
- cin >> a[i];
- gen(0);
- if (cnt == 31)
- {
- if (if1)
- cout << 0;
- else
- cout << 1;
- }
- else
- {
- cout << cnt << "\n";
- for (int i = 0; i < m; ++i)
- {
- for (int j = 0; j < ans[i]; ++j)
- cout << a[i] << " ";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement