New Year Fun cards (01 backpack + recording path)

Source: Internet
Author: User

[Problem 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 and compiledProgramAnd quickly found the missing card.

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

[Input file]

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 file]

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.

 

[Input example]

270

4

100

110

170

200

[Output example]

2 4

# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; int DP [100010], W [110]; int path [100010]; int totalw, n; void print (INT rest) {If (rest = 0) return; print (rest-W [path [rest]); printf ("% d ", path [rest]);} int main () {scanf ("% d", & totalw); scanf ("% d", & N); int sum = 0, rest, I, j; for (I = 1; I <= N; I ++) {scanf ("% d", & W [I]); sum + = W [I];} memset (DP, 0, sizeof (DP); memset (path, 0, Si Zeof (PATH); DP [0] = 1; rest = sum-totalw; for (I = 1; I <= N; I ++) for (j = rest; j> = W [I]; j --) {DP [J] = DP [J] + dp [J-W [I]; if (path [J] = 0 & DP [J-W [I]! = 0) path [J] = I; // note that path is updated only for the first time. Otherwise, the loop (J-> W [I]) following it may overwrite it! } If (DP [rest] = 0) printf ("0 \ n"); else if (DP [rest]> 1) printf ("-1 \ n "); else {print (rest); printf ("\ n");} return 0 ;}

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.