#C6963. Minimum Servers Required

    ID: 50781 Type: Default 1000ms 256MiB

Minimum Servers Required

Minimum Servers Required

You are given n servers and m data files. Each server i has a capacity of c_i and each data file j has a size of s_j. Your task is to determine the minimum number of servers needed to store all files. A file can be stored on a server if the server's remaining capacity is at least the file's size. Files are allocated one by one using a greedy approach: they are considered in descending order of their sizes and each file is placed into the first server (when servers are sorted in descending order by capacity) that is able to accommodate it. If it is not possible to store all files, print -1.

The problem can be formulated as follows:

Given integers \( n \) and \( m \), an array of server capacities \( C = [c_1, c_2, \dots, c_n] \) and an array of file sizes \( S = [s_1, s_2, \dots, s_m] \), find the minimum number of servers required such that all files can be stored if each file \( s_j \) can be placed on a server \( c_i \) if \( c_i \ge s_j \) (after subtracting sizes of previously allocated files). If it is impossible to allocate all files, output \(-1\).

inputFormat

The first line contains two integers \( n \) and \( m \) — the number of servers and the number of data files respectively.

The second line contains \( n \) space-separated integers representing the capacities of the servers.

The third line contains \( m \) space-separated integers representing the sizes of the data files.

outputFormat

Output one integer — the minimum number of servers required to store all the data files. If it is impossible to store all the files, output \(-1\).

## sample
3 4
8 4 3
5 2 2 1
2