#C8016. Sparse Matrix Multiplication

    ID: 51952 Type: Default 1000ms 256MiB

Sparse Matrix Multiplication

Sparse Matrix Multiplication

You are given two sparse matrices A and B, each represented by a list of their non-zero entries. Your task is to compute the product C = A \times B using the formula

Cij=kAikBkjC_{ij}=\sum_{k}A_{ik}\, B_{kj}

After computing the multiplication, output each non-zero element of the resulting matrix in sorted order (first by row index, then by column index). If the computed matrix is empty, output the word EMPTY.

inputFormat

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

  1. An integer nA denoting the number of non-zero entries in the first matrix A.
  2. nA lines follow, each containing three integers separated by spaces: i j v, meaning that A[i][j] = v.
  3. An integer nB denoting the number of non-zero entries in the second matrix B.
  4. nB lines follow in the same format (i j v), representing the non-zero entries of B.

outputFormat

The output is written to standard output (stdout). For each non-zero element of the resulting matrix C, print a line formatted as:

i j v

where i and j are the row and column indices, and v is the computed value. The output lines must be sorted in increasing order of i and then by j. If there are no non-zero elements in the product, output a single line with the word EMPTY.

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

1 1 4 2 2 10

</p>