#C11952. Library Transaction Fulfillment

    ID: 41325 Type: Default 1000ms 256MiB

Library Transaction Fulfillment

Library Transaction Fulfillment

You are given a library with m different books. The library initially holds a given number of copies for each book. You are then provided with a sequence of transactions. Each transaction is represented by a pair of integers: the first integer denotes the book number (1-indexed), and the second integer denotes the type of transaction: 1 for borrowing a copy and -1 for returning a copy.

When a borrowing transaction occurs, you must check if the book has at least one copy available. If it does, decrease its count by one; if it does not, the transaction cannot be fulfilled and you should output NO immediately. For a returning transaction, simply increment the number of copies available (even if there was no previous borrow, the library accepts returns).

Return YES if all borrowing transactions can be successfully carried out; otherwise, return NO.

Note: The transactions are processed in the order given.

Mathematically, if we denote the available copies of book i at time t by \(a_i(t)\), then for a borrowing transaction (\(a_i(t) > 0\)) we update \(a_i(t+1) = a_i(t) - 1\), otherwise output \(NO\). For a returning transaction, \(a_i(t+1) = a_i(t) + 1\).

inputFormat

The input is read from stdin and is structured as follows:

  • The first line contains an integer m — the number of different books.
  • The second line contains m space-separated integers, which represent the initial number of copies available for each book.
  • The third line contains an integer n — the number of transactions.
  • The following n lines each contain two integers. The first integer is the book number (1-indexed) and the second integer is the transaction type (either 1 for borrowing or -1 for returning).

outputFormat

Output a single line to stdout containing either YES if all borrow transactions have been fulfilled successfully, or NO if any borrow transaction failed because the requested book did not have enough copies available at the time of the request.

## sample
5
4 2 1 0 3
6
1 1
2 1
3 1
5 -1
2 1
4 1
NO