Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program zad_c;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- const
- MAXN = 1000;
- var
- a: array [0..MAXN] of integer;
- i, j, m, n, k, ind, ans: integer;
- procedure rec(sum, ind, k: integer);
- begin
- if (ind > m) then
- exit;
- if (sum = n) and (k < ans) then
- ans := k
- else
- if (sum > n) and (ans = maxint) then
- ans := 0
- else
- begin
- rec(sum, ind + 1, k);
- rec(sum + a[ind], ind + 1, k + 1);
- rec(sum + a[ind] * 2, ind + 1, k + 2);
- end;
- end;
- begin
- read(n, m);
- for i := 1 to m do
- read(a[i]);
- ans := maxint;
- rec (0, 1, 0);
- if (ans = maxint) then
- writeln('-1')
- else
- writeln(ans);
- readln;readln;readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement