#C3630. Maximum Total Duration Under Limit

    ID: 47079 Type: Default 1000ms 256MiB

Maximum Total Duration Under Limit

Maximum Total Duration Under Limit

You are given a maximum allowed duration N, an integer representing the number of messages M, and a list of M message durations. Your task is to determine the maximum total duration that can be achieved by selecting a subset of messages such that their combined duration does not exceed N.

This is a subset sum / knapsack problem where you must choose some durations whose sum is as large as possible without exceeding the given limit N. Formally, if you denote the selected durations by a subset S, then you need to maximize

iSdi\sum_{i \in S} d_i

subject to

iSdiN.\sum_{i \in S} d_i \leq N.

inputFormat

The input is given via standard input (stdin) and consists of two lines:

  • The first line contains two space-separated integers: N (the maximum allowed total duration) and M (the number of messages).
  • The second line contains M space-separated integers representing the durations of the messages.

outputFormat

Output a single integer to standard output (stdout) denoting the maximum total duration that can be achieved without exceeding the limit N.

## sample
10 3
4 5 1
10