Vijos p1071 New Year Fun cards

Source: Internet
Author: User
Description

During the Chinese New Year, the favorite activity of the adults was playing cards. Xiaomengxian does not play cards, so he has to sit and watch.

On this day, when a group of people played cards, they suddenly shouted: "This deck is missing a few !" There are fewer people. "This is a special card. I know the weight of each card. As long as we name the total weight of the remaining cards, we can know which cards are missing ." Everyone thinks this method is good, so they name the total weight of the remaining cards and start to calculate which cards are missing. Since the data volume is large, after a while, everyone is dizzy.

At this moment, xiaomengxian said, "Look at me !" So he took out his laptop, compiled a program, and quickly found out the missing cards.

What if this happens to you? Can you do the same thing?

Format input format

The totalw integer in the first line indicates the total weight of the remaining cards.

The second line contains an integer N (1 <n <= 100), which indicates the number of cards.

In the next n rows, each line has an integer wi (1 <= wi <= 1000), indicating the weight of each card.

Output Format

If there is no solution, "0" is output; If there are multiple solutions, "-1" is output; otherwise, the number of the lost card is output in ascending order, two Adjacent numbers are separated by a space.

Example 1 input 1 [copy]
2704100110170200
Sample output 1 [copy]
2 4
Restrictions

1 s for each test point

Prompt

Sample input #2
270
4
100
110
160
170

Sample output #2
-1

Sample input #3
270
4
100
120
160
180

Sample output #3
0

AC code:
 1 var n,i,j,tot,ans:longint; 2     a,f,g:array[0..10000] of longint; 3     p:array[0..10000]of boolean; 4 begin 5 readln(tot); 6 readln(n);f[0]:=1; 7 for i:=1 to n do readln(a[i]); 8 for i:=1 to n do 9  for j:=tot downto a[i] do10   begin11   if (f[j-a[i]]>0) and (f[j]=0) then g[j]:=i;12   f[j]:=f[j]+f[j-a[i]];13   end;14 i:=tot;15 if f[tot]>1 then begin writeln(‘-1‘);halt;end;16 if f[tot]=0 then begin writeln(‘0‘);halt;end;17 while (i>0) and (g[i]<>0) do18  begin19  p[g[i]]:=true;20  i:=i-a[g[i]];21  end;22 for i:=1 to n do if not p[i] then write(i,‘ ‘);23 end.                  
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.