#K54982. Character Carrying Capacity

    ID: 29874 Type: Default 1000ms 256MiB

Character Carrying Capacity

Character Carrying Capacity

You are given n characters, each with a maximum weight limit. There are m items with specified weights. The items are assigned in order to the characters. A character can carry the next item if the total weight of items carried does not exceed his limit. Otherwise, the next character starts carrying items from that point on. If at any point the current item is heavier than the current character’s remaining capacity, the process stops, and the answer is NO.

Formally, let the characters have weight limits \(L_1, L_2, \dots, L_n\) and the items have weights \(w_1, w_2, \dots, w_m\). The assignment proceeds sequentially: the first character carries a consecutive sequence \(w_1, w_2, \dots, w_k\) such that \(\sum_{i=1}^{k} w_i \le L_1\) and \(\sum_{i=1}^{k+1} w_i > L_1\). Then the next character attempts to carry from \(w_{k+1}\), and so on. If all items are assigned successfully, output YES; otherwise, output NO.

inputFormat

The input is given in the following format from stdin:

n
L1 L2 ... Ln
m
w1 w2 ... wm

Where:

  • n is the number of characters.
  • L1, L2, ..., Ln are the weight limits of the characters.
  • m is the number of items.
  • w1, w2, ..., wm are the weights of the items in the specified order.

outputFormat

Output a single line to stdout containing either YES if all items can be assigned to the characters without any of them exceeding their weight limits or NO otherwise.

## sample
2
5 10
4
2 2 4 3
YES