#C10596. Max Delivered Messages

    ID: 39818 Type: Default 1000ms 256MiB

Max Delivered Messages

Max Delivered Messages

You are given n messages and a system schedule of length m. Each message i requires a certain number of online time slots (given by an integer in the array delivery_times) to be delivered. The system schedule is represented by an array schedule of length m where a value of 1 indicates that the system is online at that time slot and 0 indicates offline.

Your task is to determine the maximum number of messages that can be delivered, assuming that you may deliver the messages in increasing order of their required delivery time. Formally, let \(d_1, d_2, \dots, d_n\) be the sorted delivery times (in ascending order). Let \(A\) be the total number of online slots (i.e. the sum of the elements of schedule). A message with delivery requirement \(d\) can be delivered if the sum of the delivery requirements of all messages delivered before plus \(d\) does not exceed \(A\).

Note: All formulas are written in \(\LaTeX\) format. For example, the condition for delivering \(k\) messages is:

\[ \sum_{i=1}^{k} d_i \leq A \]

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains two integers n and m separated by a space, where n is the number of messages and m is the length of the schedule.
  • The second line contains n space-separated integers representing the delivery_times of the messages.
  • The third line contains m space-separated integers (each either 0 or 1) representing the schedule.

outputFormat

Output a single integer representing the maximum number of messages that can be delivered.

The output should be printed to standard output (stdout).

## sample
3 10
4 2 5
1 1 0 0 1 1 1 0 0 1
2