Under construction

Still being built. Things may change

The Eating Puzzle

Interview

Time limit1sMemory limit128 MB

Summary
Given up to 21 bucket sizes and a calorie limit, choose a subset with the largest sum that does not exceed the limit.
Level

Medium4 of 10

Topics
Brute force, Bit manipulation, Array, Recursion
Solved
No attempts yet

Problem

Bessie is on a diet and may eat no more than CC (10≤C≤3500010 \le C \le 35000) calories per day. To tease her, Farmer John sets out BB (1≤B≤211 \le B \le 21) buckets of feed, each holding some number of calories (each value is between 11 and 3500035000, and the values need not be distinct). Bessie has no self-control: once she starts on a bucket, she eats all of it.

Bessie is not good at combinatorics. Determine the combination of feed buckets that lets her eat as many calories as possible without exceeding the limit CC, and report that number of calories.

Input

  • Line 1: Two space-separated integers, CC and BB.
  • Line 2: BB space-separated integers giving the calories in bucket 1, bucket 2, and so on.

Output

  • Line 1: A single integer — the largest number of calories Bessie can consume while staying on her diet.

Examples1

  1. Example 1

    Input
    40 6
    7 13 17 19 29 31
    
    Expected output
    39