#C7184. Total Sales Aggregation

    ID: 51027 Type: Default 1000ms 256MiB

Total Sales Aggregation

Total Sales Aggregation

You are given a list of sales records and a set of filter criteria. Each sales record contains the following fields:

  • date: a string in the format \( YYYY-MM-DD \)
  • country: a string representing the country
  • product: a string representing the product code
  • amount: an integer representing the sale amount

You are also given a number of filter criteria. For every record, if the record satisfies all the criteria (i.e. for every key in the criteria, its value equals the corresponding value in the record), then the record is counted. Your task is to compute the sum of the amounts of all such records.

The filtering condition can be expressed mathematically as follows:

\[ \text{answer} = \sum_{i=1}^{n} \text{amount}_i \cdot \bigwedge_{(k,v) \in \text{criteria}} [record_i(k)=v] \]

inputFormat

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

  1. An integer \( n \) denoting the number of sales records.
  2. \( n \) lines follow, each containing a sales record with four fields: date (string), country (string), product (string) and amount (integer), separated by spaces.
  3. An integer \( m \) denoting the number of filter criteria.
  4. \( m \) lines follow, each containing a key and a value separated by a space representing one criterion. If \( m \) is zero, then no filtering is applied and all records are counted.

outputFormat

Output a single integer representing the total sales amount for all records that satisfy the given criteria. The result is printed to standard output.

## sample
6
2023-01-01 USA A 100
2023-01-02 CAN B 150
2023-01-02 USA A 200
2023-01-03 USA B 300
2023-01-03 CAN A 50
2023-01-04 USA C 500
0
1300

</p>