#C8399. Container Selection Challenge

    ID: 52376 Type: Default 1000ms 256MiB

Container Selection Challenge

Container Selection Challenge

In this problem, you are given a sequence of containers with specified weights and a crane that has a maximum capacity (C). The containers must be loaded in order from the beginning of the sequence. Starting with an initial sum of 0, you add the weight of each container sequentially. However, if adding the next container would cause the total weight to exceed (C), you must stop. Your task is to output the number of containers that can be loaded without exceeding the capacity. If no container can be loaded (i.e. the first container itself is too heavy), output (-1).

Formally, given an integer (n), an array of weights (w_1, w_2, \dots, w_n), and a capacity (C), find the largest integer (k) (with (0 \le k \le n)) such that (\sum_{i=1}^{k} w_i \le C). If (k = 0), print (-1).

inputFormat

The input is given via standard input (stdin) and has the following format:

  • The first line contains a single integer (n) representing the number of containers.
  • The second line contains (n) space-separated integers representing the weights of the containers.
  • The third line contains a single integer (C), the maximum capacity of the crane.

outputFormat

Output a single integer via standard output (stdout) representing the number of containers that can be loaded sequentially without the total weight exceeding (C). If no container can be loaded, output (-1).## sample

5
2 5 4 8 3
10
2